使用 jQuery 基于所选值调用键关闭事件



我想根据下拉列表中的用户选择仅使输入文本为数字或不(如果用户选择客户代码或费率请求 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:
    }
});​

只是出于兴趣,我只是自己写了其中之一,如果有兴趣,如下所示

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

允许使用数字键盘和主键盘编号,以及左箭头,右箭头,退格键,制表键和删除键

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

工作演示

多亏了@thecodeparadox我结束了以下工作:演示

网页代码:

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

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

感谢您的帮助:)

相关内容

  • 没有找到相关文章

最新更新