Onfocus和离焦文本框

时间:2013-10-03 18:55:14

标签: jquery html

我编写了一个jquery脚本,可以自动检测用户输入的数字。如果用户输入了2,4,6,8,10 - 他们会自动弹出一个确认框,说明有报价。

如果他们点击“是”,则会重定向。但是,如果他们单击“取消”,则会将其重点放回文本框,并再次显示确认框。

无论如何,如果用户点击CANCEL,我可以这样做,焦点不在文本框中,因此不会出现循环确认框。

HTML

<input type="text" class="Quantity" id="Quantity9438851" name="Quantity" value="1" style="width:30px">

JQUERY

$("#Quantity9438851").bind("change paste keyup", function() {
    if($(this).val() == 2){
     var didConfirm = confirm("There is currently an offer for 2 polo shirt for £25. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://blahblah.com/&Quantity=10";
      }
    }else if($(this).val() == 4){
     var didConfirm = confirm("There is currently an offer for 4 polo shirt for £50. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://google.com/Registration/blah";
      }        
    }else if($(this).val() == 6){
     var didConfirm = confirm("There is currently an offer for 6 polo shirt for £75. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://google.com/Registration/blah";
      }                
    }else if($(this).val() == 8){
     var didConfirm = confirm("There is currently an offer for 8 polo shirt for £100. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://google.com/Registration/blah";
      }                
    }else if($(this).val() == 10){
     var didConfirm = confirm("There is currently an offer for 10 polo shirt for £100. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://google.com/Registration/blah";
      }                
    }
});

http://jsfiddle.net/NUJrL/3/

1 个答案:

答案 0 :(得分:1)

为什么不简单地使用blur事件?

$("#Quantity9438851").bind("blur", function() {

    var val = $(this).val();

    if(!(val % 2) && val <= 10){

      var conf = confirm("There is currently an offer for "
         + val +" polo shirt for £25. Click YES to recieve discount.");

      if (conf) 
        window.location.href = "http://google.com/&Quantity=10";


    }
});

http://jsfiddle.net/NUJrL/6/