简体   繁体   English

页面加载后如何执行if语句?

[英]How to execute if statement after page has been loaded?

see it in action here: http://jsbin.com/relamiqeru/1/ 在此处查看其运行情况: http : //jsbin.com/relamiqeru/1/

I am trying to make it so that if a user enters "idh4" into the coupon code field, my "booyah" div gets filled with that html provided. 我正在尝试这样做,以便如果用户在优惠券代码字段中输入“ idh4”,则我的“ booyah” div会被提供的html所填充。

The issue I feel, is that the javascript is only executed when the page is loaded, so even if idh4 is entered into the field, its not checking it anymore. 我感觉到的问题是,仅在加载页面时才执行javascript,因此即使将idh4输入到字段中,它也不会对其进行检查。 Perhaps an if statement is not the right thing to use in this case? 也许if语句在这种情况下不适合使用?

At request, here is the jsbin code: 根据要求,这是jsbin代码:

  <div id="booyah"></div>
<form action="" method="POST">
        Coupon Code?<input id="couponcode" type="text" name="coupon code" value="idh4"><br>
        <input type="submit">
    </form>

$("#couponcode").change(function(){
if ($("#couponcode").val() === 'idh4') {
      console.log('it got called');
      $( "#booyah" ).html("<p>That code gets you free snappie stickers!</p>");
  }

  });

I'm not sure this is what you want, but you can take a look at it anyway : 我不确定这是否是您想要的,但是无论如何您都可以看一下:

$(document).ready(function(){
   $("#booyahInput").on('keyup', function(){
      if ($(this).val() == 'idh4') {
         console.log('it got called');
         $( "#booyah" ).html("<p>That code gets you free snappie stickers!</p>");
      }
    });
});

http://jsfiddle.net/rwxygptf/ http://jsfiddle.net/rwxygptf/

Here problem is you have used change event on textbox: 这里的问题是您在文本框上使用了change事件:

$("#couponcode").change

On textbox change gets fired on losing focus. 在文本框上, change失去了焦点。 So after entering coupon user has to go off the field. 因此,输入优惠券后,用户必须离开该字段。

Instead try input , keyup , keypress events for immediate response. 而是尝试使用inputkeyupkeypress事件进行即时响应。

$("#couponcode").on('input', function(){
    //
});

If you want it on submit, you have to add a function to fire on submit, if you want it to fire on text changing in the field - You need to listen to events. 如果要在提交时触发,则必须添加一个函数在提交时触发,如果要在提交的字段中更改文本时触发,则需要听事件。 Understanding event listeners is key to understanding javascript. 了解事件监听器是了解javascript的关键。

Here are 2 references: Submit: 这里有2个参考:提交:

http://www.javascript-coder.com/html-form/html-form-tutorial-p1.phtml http://www.javascript-coder.com/html-form/html-form-tutorial-p1.phtml

Listener: 听众:

jQuery Listen For All Text Input Changes jQuery侦听所有文本输入的更改

The problem you are having, is when the form is submitted the page is reloading. 您遇到的问题是,提交表单后,页面正在重新加载。 To do this properly you will need to submit the form through AJAX and prevent a page refresh. 为此,您需要通过AJAX提交表单,并防止页面刷新。 But with the code you provided it's not clear if you're doing that. 但是使用您提供的代码尚不清楚是否正在执行此操作。

JSFiddle JSFiddle

$("#myform").on("submit", function(e) {
    e.preventDefault();
    if ($("#couponcode").val() === 'idh4') {
        console.log('it got called');
        $("#booyah").html("<p>That code gets you free snappie stickers!</p>");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM