【问题标题】:Delete text in input field not working删除输入字段中的文本不起作用
【发布时间】:2016-06-20 21:43:22
【问题描述】:

我有一个加号/减号按钮,您可以在其中仅在输入字段中插入数值,或单击加号/减号按钮选择一个值。当您在文本字段内单击并尝试删除或退格它不起作用的数字时。这是 html 和我的小提琴的链接。更新:删除键在 Chrome 中有效,但在 Firefox 中无效。 https://jsfiddle.net/714dxayk/5/

<span class="form-title">Quantity</span>
<form id='myform' method='POST' action='#' class="numbo">
<input type='button' value='-' class='qtyminus' field='quantity' style="font-weight: bold; font-size:18px;" />
<input type='text' name='quantity' value='1' class='qty' style="margin-bottom: 0px !important" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
<input type='button' value='+' class='qtyplus' field='quantity' style="font-weight: bold; font-size:18px;" />
</form>

【问题讨论】:

  • 我没有看到那里的脚本,你怎么能说不工作?
  • 我没有放 javascript,因为这不是我要解决的问题。
  • 如果有帮助的话,我用 javascript 编辑了我的小提琴。
  • onkeypress='return event.charCode &gt;= 48 &amp;&amp; event.charCode &lt;= 57' 看起来会拒绝所有非数字按键。
  • 你为什么使用键码而不是实际值?

标签: javascript html input


【解决方案1】:

尝试为输入字段分配按键处理程序。在处理程序中,让浏览器处理特定的按键并忽略其他按键:

$('.qty').keypress(function(ev){
    // Don't ignore numbers.
    if (ev.charCode >= 48 && ev.charCode <= 57) {
        return;
    }

    // Don't ignore BS, DEL, and arrow keys.
    switch (ev.keyCode) {
        case 8:   // backspace
        case 46:  // delete key
        case 37:  // arrows
        case 38:
        case 39:
        case 40:
            return;
    }

    // Ignore the rest of the keys.
    ev.preventDefault();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多