【发布时间】:2016-12-02 11:50:55
【问题描述】:
根据我的要求,用户只能从页面上提供的键盘输入数字。所以输入字段是只读的。
现在我想得到的是,当用户开始输入月份时,其他文本应该保留在文本字段中,直到用户输入。例如05/DD/YYYY 像这样。因此,该文本将被隐藏。
如果我放置了占位符,那么当用户开始输入数字时,所有文本都消失了。我不想要那个。所以我在单独的span tag 中使用了“MM/DD/YYYY”文本。
var Memory = "0", // initialise memory variable
Current = "", // and value of Display ("current" value)
Operation = 0, // Records code for eg * / etc.
MAXLENGTH = 8; // maximum number of digits before decimal!
function format(input, format, sep) {
var output = "";
var idx = 0;
for (var i = 0; i < format.length && idx < input.length; i++) {
output += input.substr(idx, format[i]);
if (idx + format[i] < input.length) output += sep;
idx += format[i];
}
output += input.substr(idx);
return output;
}
function AddDigit(dig) { //ADD A DIGIT TO DISPLAY (keep as 'Current')
if (Current.indexOf("!") == -1) { //if not already an error
if ((eval(Current) == undefined) &&
(Current.indexOf(".") == -1)) {
Current = dig;
document.calc.display.focus();
} else {
Current = Current + dig;
document.calc.display.focus();
}
Current = Current.toLowerCase(); //FORCE LOWER CASE
} else {
Current = "Hint! Press 'Clear'"; //Help out, if error present.
}
if (Current.length > 0) {
Current = Current.replace(/\D/g, "");
Current = format(Current, [2, 2, 4], "/");
}
document.calc.display.value = Current.substring(0, 10);
document.getElementById("cursor").style.visibility = "hidden";
}
function Clear() { //CLEAR ENTRY
Current = "";
document.calc.display.value = Current;
document.calc.display.focus();
document.getElementById("cursor").style.visibility = "visible";
//setInterval ("cursorAnimation()", 5000);
}
function backspace() {
Current = document.calc.display.value;
var num = Current;
Current = num.slice(0,num.length - 1);
document.calc.display.value = Current;
document.calc.display.focus();
document.getElementById("cursor").style.visibility = "hidden";
}
function cursorAnimation() {
$("#cursor").animate({
opacity: 0
}, "fast", "swing").animate({
opacity: 1
}, "fast", "swing");
}
//--------------------------------------------------------------->
$(document).ready(function() {
document.getElementById("cursor").style.visibility = "visible";
//setInterval ("cursorAnimation()", 1000);
});
.intxt1 {
padding: 16px;
border-radius: 3px;
/* border: 0; */
width: 1017px;
border: 1px solid #000;
font-family: Droid Sans Mono;
background: #fff;
}
.txtplaceholder {
font-family: "Droid Sans Mono";
color: #D7D7D7;
position: relative;
float: left;
left: 219px;
top: 17px;
z-index: 10 !important;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
display: inline-block;
}
#cursor {
position: relative;
z-index: 1;
left: 32px;
top: 2px;
visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<form Name="calc" method="post">
<div style="position:relative">
<span id="cursor">_</span>
<span class="txtplaceholder">MM/DD/YYYY</span>
<span style="z-index:100">
<input class="intxt1" autocomplete="off" id="pt_dob" name="display" value="" type="text" readonly>
</span>
<button class="cancel-icon" type="reset" onClick="Clear()"></button>
</div>
<div class="num_keypad1" style=" margin-top:19px;">
<!-- Screen and clear key -->
<div class="num_keys">
<!-- operators and other keys -->
<span id="key1" onClick="AddDigit('1')">1</span>
<span id="key2" onClick="AddDigit('2')">2</span>
<span id="key3" onClick="AddDigit('3')">3</span>
<span id="key4" onClick="AddDigit('4')">4</span>
<span id="key5" onClick="AddDigit('5')">5</span>
<span id="key6" onClick="AddDigit('6')">6</span>
<span id="key7" onClick="AddDigit('7')">7</span>
<span id="key8" onClick="AddDigit('8')">8</span>
<span id="key9" onClick="AddDigit('9')">9</span>
<span id="key0" onClick="AddDigit('0')" style="width: 200px;">0</span>
<span id="keyback" class="clear" onClick="backspace()"> <div class="num_xBox">X</div></span>
</div>
</div>
</form>
问题如下:
我的数字低于文本“MM/DD/YYYY”。我不知道我应该如何让我的数字高于该文本
我应该如何隐藏用户输入的文本并相应地显示其他文本,例如如果用户输入 05 并显示类似“05/DD/YYYY”的其他文本,“MM”应该隐藏。
谁能帮帮我?
注意:使用 input type=date 或任何其他插件,我可以实现上述功能,但我的要求不同。我必须仅使用 HTML、CSS、JS 来实现这一点。
【问题讨论】:
-
您正在寻找的效果是“输入屏蔽”。如果你用谷歌搜索,已经有数百个插件可以为你做到这一点
-
是的,我知道插件,但我的要求不同。我不能使用默认插件,因为我的输入字段是只读的,用户必须从页面上提供的键盘输入数字。我在我的问题中也提到了这一点。因为我只能使用提供的键盘,所以我已经管理了 Javascript 中的所有功能(问题中提到)。
-
我读了你的问题。这些限制都不会影响屏蔽插件的使用
-
我见过的插件,用户可以直接从他们的本地键盘在文本框中输入数字。但在我的情况下,不允许用户从他们的本地键盘输入数字。因此,我将输入文本框设为只读,并应用 JS 从键盘输入数字。用户必须使用提供的键盘(如图所示)
标签: javascript jquery html css date