防止在Jquery

时间:2017-02-27 13:01:28

标签: jquery keypress keycode backspace

我有一个文本框。当用户按下退格键时,我想阻止退格键删除文本并提供特定的提醒,例如"你想删除"?还有两个按钮。只有当用户确认删除操作时,后续的退格按钮才会删除文本。

我写了下面的代码。虽然它工作正常。但它无法阻止退格删除文本。任何人都可以帮助我吗?



$("#txtCustNameSHP").keyup(function (e) {
    var key = event.keyCode || event.charCode;
    if (keypresscount == 0 && key == 8 || key == 46) {
        event.preventDefault();
        $.uxMsg({
            title: "Confirmation",
            msg: "Do you want to delete the Shipper?",
            button1Text: "YES",
            button2Text: "NO",
            button1Click: function () {
                // CUSTOMER LOV
                //$("#" + txtCustCodeSHP).text("");                        
                keypresscount++;
            },
            button2Click: function () {
                return false;
            },
            width: "350px",
            height: "100px"
        });
    }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="txtCustNameSHP" value="foobar" />
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:4)

First Thing-将回调处理程序绑定到事件 keydown ,而不是 keyup ,因为您希望在单击按钮时捕获该按钮而不是之后。然后使用e.which检查按下了哪个键。来自the jQuery documentation

  

要确定按下了哪个键,请检查该事件对象   传递给处理函数。虽然浏览器使用不同   存储此信息的属性,jQuery规范化.which   属性,因此您可以可靠地使用它来检索密钥代码。

https://api.jquery.com/keydown/

$("#txtCustNameSHP").keydown( function(e){  
  if( e.which == 8){   
    e.preventDefault();  
    return false;   
  } 
});  

答案 1 :(得分:4)

问题1:您应该使用keydown代替keyup,因为在删除已经完成后会触发密钥更新

问题2:您的活动为e而不是event

&#13;
&#13;
$("#txtCustNameSHP").keydown(function (e) {
  var key = e.keyCode || e.charCode;
  if (key == 8 || key == 46) {
      e.preventDefault();
      e.stopPropagation();
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="txtCustNameSHP" value="foobar" />
&#13;
&#13;
&#13;

使用确认消息:

&#13;
&#13;
$("#txtCustNameSHP").keydown(function(e) {
    var key = e.keyCode || e.charCode;
    if (key == 8 || key == 46) {
        if (typeof(dontDelete) == "undefined") {
            var r = confirm("You're about to delete!\nAre you sure?");
            if (r == true) {
                dontDelete = false;
            } else {
                dontDelete = true;
            }
        }
        if (dontDelete) {
            e.preventDefault();
            e.stopPropagation();
        }
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="txtCustNameSHP" value="foobar" />
<button type="button" onclick="dontDelete = false">enable delete</button>
<button type="button" onclick="dontDelete = true">disable delete</button>
<button type="button" onclick="dontDelete = undefined">Reset (ask on delete)</button>
&#13;
&#13;
&#13;

选项:

  • dontDelete = undefined;将向用户显示确认 并且选择将记住
  • dontDelete = false;用户将可以删除。
  • dontDelete = true;用户将 NOT 能够删除。

答案 2 :(得分:2)

&#13;
&#13;
$('#input').keydown(function(e) {
  if(e.which == 8) {
    var conf = confirm('Do you want to delete the Shipper?')
    if(!conf) e.preventDefault();
    else $(this).val('');
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="input" value="foobar" />
&#13;
&#13;
&#13;