【发布时间】:2019-04-01 11:47:25
【问题描述】:
我遇到了一种情况,我正在使用 jquery 验证输入类型。
我有 html 下拉列表,其中包含不同的参数("select:first-child")。
我正在尝试根据这些参数验证输入框,为此我在 jquery 中编写了以下代码。
例如-
如果我选择“数量”,那么输入框应该只输入数字。
如果我选择“TradeDate”,那么输入框应该是日期。
现在的问题是,当我选择类型为 date 的参数时,datepicker 会出现选择日期。 但是当我选择任何其他类型为数字的参数时,输入仍然显示日期选择器。
所以,我错了,我每次都想要这个验证
这里 var type[1] 包含参数的类型,例如。浮点数、日期、字符等
$("#cond_div").children("select:first-child").change(function(event){
var temp = $(this).val();
var type = temp.split("_");
$("#cond_div").children("input").on("keypress keyup", function () {
if (type[1].localeCompare("date") == 0) {
$(this).datepicker();
}
else if (type[1].localeCompare("float") == 0) {
$(this).val($(this).val().replace(/[^0-9\.]/g, ''));
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
}
else if (type[1].localeCompare("int") == 0) {
$(this).val($(this).val().replace(/[^0-9\.]/g, ''));
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
}
});
});
【问题讨论】:
标签: javascript jquery html jquery-ui