0

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

3 回答 3

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

允许小键盘、主键盘数字、左箭头、右箭头、退格键、制表键和删除键

于 2012-09-17T04:43:28.403 回答
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:
    }
});

工作演示

于 2012-09-17T05:09:48.757 回答
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:
    }
});​

其实我不知道这是更好的方法还是最有效的方法,所以仍然欢迎建议

谢谢您的帮助 :)

于 2012-09-17T21:17:28.870 回答