我只想根据用户从下拉列表中的选择输入文本或不输入数字(如果用户选择客户代码或费率请求 ID,则文本框仅为数字,如果是客户代码可以写字母)。这是我编写但无法正常工作的代码,有什么帮助吗? jsfiddle代码
<select id="test" class="target">
<option value="custName">Customer Name</option>
<option value="custCode">Customer Code</option>
<option value="rateReqId">Rate Request Id</option>
</select>
<input id="target" type="text" />
Javascript代码是:
// Numeric only control handler
jQuery.fn.ForceNumericOnly =
function() {
return this.each(function() {
$(this).keydown(function(e) {
var key = e.charCode || e.keyCode || 0;
// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
return (
key == 8 || key == 9 || key == 46 || (key >= 37 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105));
});
});
};
$('#test').change(function() {
var val = this.value;
switch (val) {
case "custName":
$("#target").unbind('ForceNumericOnly')
break;
case "custCode":
alert('inside custCode');
$("#target").ForceNumericOnly();
break;
case "rateReqId":
break;
default:
}
});