使用jquery调用基于选定值的keydown事件

时间:2012-09-17 04:33:07

标签: javascript jquery javascript-events client-side onkeydown

我想从下拉列表中选择一个输入文本数字或不选择用户选择(如果用户选择客户代码或费率请求ID,则文本框仅为数字,如果客户代码可以写字母)。 这是我写的代码但是没有用,有什么帮助吗? jsfiddler code

<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:
    }
});​

3 个答案:

答案 0 :(得分:0)

出于兴趣,我只是写下了我自己的一个,如果感兴趣的话,如下所示

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;
    }
}

允许使用小键盘,主键盘编号,左箭头,右箭头,退格键,制表符和删除键

答案 1 :(得分:0)

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:
    }
});

The working demo

答案 2 :(得分:0)

感谢@thecodeparadox我结束了以下操作: demo

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>

jquery代码:

// 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:
    }
});​

实际上我不知道这是更好的方法还是最有效的方法,所以仍然愿意接受建议

感谢您的帮助:)