Javascript 使用jQuery根据所选值调用keydown事件

Javascript 使用jQuery根据所选值调用keydown事件,javascript,jquery,client-side,jquery-events,onkeydown,Javascript,Jquery,Client Side,Jquery Events,Onkeydown,我想根据用户从下拉列表中选择的内容(如果用户选择客户代码或费率请求Id,则文本框仅为数字,如果是客户代码,则文本框可以写字母),使输入文本仅为数字。 这是我写的但不起作用的代码,有什么帮助吗? 客户名称 客户代码 费率请求Id Javascript代码是: // Numeric only control handler jQuery.fn.ForceNumericOnly = function() { return this.each(function() { $

我想根据用户从下拉列表中选择的内容(如果用户选择客户代码或费率请求Id,则文本框仅为数字,如果是客户代码,则文本框可以写字母),使输入文本仅为数字。 这是我写的但不起作用的代码,有什么帮助吗?


客户名称
客户代码
费率请求Id
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:
    }
});​
//仅限数字的控件处理程序
jQuery.fn.ForceNumericOnly=
函数(){
返回此值。每个(函数(){
$(此).keydown(函数(e){
var key=e.charCode | | e.keyCode | | 0;
//仅允许退格、制表符、删除、箭头、数字和键盘数字
返回(

键==8 | |键==9 | |键==46 | | |(键>=37&&key=48&&key=96&&key只是出于兴趣,我自己写了其中一个,如果感兴趣的话,如下所示

function numbersOnlyTextBox(e) {
     var NumberFieldCharCodes = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 8, 9, 13, 46, 37, 39];
    if (e.shiftKey) {
        e.preventDefault();
    }
    if (!~$.inArray(e.which, NumberFieldCharCodes)) {
        return false;
    }
}
允许输入numpad和主键盘数字,以及左箭头、右箭头、退格键、制表键和删除键=
jQuery.fn.ForceNumericOnly =

function() {
    return this.each(function() {
        $(this).keydown(function(e) {
            var key = e.charCode || e.keyCode || 0;
            return (
            key == 8 || key == 9 || key == 46 || (key >= 37 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105));
        });
    });
};

/**
 * as plugin functionality is not undoable
 * so one alternative solution is to make a
 * clone of the element without data and events.
 *
 * After binding plugin function that element
 * replace the existing element with cloned
 * element to remove that plugin functionality
 */

var target = $('#target'),
    clonedTarget = $('#target').clone(false, false);

$('#test').change(function() {
    var val = this.value;
    switch (val) {
    case "custName":
        target.replaceWith(clonedTarget);
        break;
    case "custCode":
        target.val('').ForceNumericOnly();
        break;
    case "rateReqId":
        target.val('').ForceNumericOnly();
        break;
    default:
    }
});
函数(){ 返回此值。每个(函数(){ $(此).keydown(函数(e){ var key=e.charCode | | e.keyCode | | 0; 返回(
key==8 | | key==9 | | key==46 | | |(key>=37&&key=48&&key=96&&key多亏了@thecodeparadox,我结束了以下工作:

html代码:

<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" />

<div id="toolTip" style="border: solid 1px #000; padding: 2px 2px 2px 2px; background-color: #f1f1f1; width: 150px; height: 30px; font-size: 11px; position: absolute; top: 250px;
    left: 130px; display: none;">test
</div>
// Numeric only control handler
jQuery.fn.ForceNumericOnly =

function(selectedFilter) {
    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
            var result = (
            key == 8 || key == 9 || key == 46 || (key >= 37 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105));

            if (!result) {
                var tooltip = $('#toolTip');

                if (!tooltip.is(':visible')) {
                    tooltip.text("Numeric values only for " + selectedFilter);
                    tooltip.show().fadeOut(3000);
                }

            }
            return result;
        });
    });
};

$('#test').change(function() {
    var val = this.value;
    var input = $("#target");
    switch (val) {
    case "custName":
        input.val('').unbind();
        break;
    case "custCode":
        input.val('').unbind().ForceNumericOnly("Customer Code");
        break;
    case "rateReqId":
        input.val('').unbind().ForceNumericOnly("Rate Request Id");
        break;
    default:
    }
});​

客户名称
客户代码
费率请求Id
测试
jquery代码:

<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" />

<div id="toolTip" style="border: solid 1px #000; padding: 2px 2px 2px 2px; background-color: #f1f1f1; width: 150px; height: 30px; font-size: 11px; position: absolute; top: 250px;
    left: 130px; display: none;">test
</div>
// Numeric only control handler
jQuery.fn.ForceNumericOnly =

function(selectedFilter) {
    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
            var result = (
            key == 8 || key == 9 || key == 46 || (key >= 37 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105));

            if (!result) {
                var tooltip = $('#toolTip');

                if (!tooltip.is(':visible')) {
                    tooltip.text("Numeric values only for " + selectedFilter);
                    tooltip.show().fadeOut(3000);
                }

            }
            return result;
        });
    });
};

$('#test').change(function() {
    var val = this.value;
    var input = $("#target");
    switch (val) {
    case "custName":
        input.val('').unbind();
        break;
    case "custCode":
        input.val('').unbind().ForceNumericOnly("Customer Code");
        break;
    case "rateReqId":
        input.val('').unbind().ForceNumericOnly("Rate Request Id");
        break;
    default:
    }
});​
//仅限数字的控件处理程序
jQuery.fn.ForceNumericOnly=
功能(selectedFilter){
返回此值。每个(函数(){
$(此).keydown(函数(e){
var key=e.charCode | | e.keyCode | | 0;
//仅允许退格、制表符、删除、箭头、数字和键盘数字
var结果=(

key==8 | | key==9 | | key==46 | |(key>=37&&key=48&&key=96&&key,而不是preventDefault(),尝试返回false,这将停止按键谢谢您的快速回复,但我对jquery是新手,我想知道如何仅在使用custName时分离forcenumerico,因为在这种情况下,我希望允许字母thnaks