简体   繁体   English

为什么这个 onchange function 不适用于 option:selected?

[英]Why does this onchange function not work with option:selected?

This has to be simple: why does this on change function not work with option:selected ?这必须很简单:为什么更改 function 不能与option:selected一起使用?

And, why does my variable state not "reload" on change of the option?而且,为什么我的变量state在更改选项时不会“重新加载”?

 var states = ["AL", "AK", "AZ", "VT"]; $('#shipping_state').on('change', function() { var state = $("#shipping_state option:selected").text(); console.log(state); jQuery("#shipping_state_field").append("<div id=\"no-ship-state\">Sorry, due to " + state + " state law, alcohol can't be shipped to a " + state + " address</div>"); if (states.includes($(this).val())) { jQuery("#no-ship-state").show(); } else { jQuery("#no-ship-state").hide(); } });
 #no-ship-state { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <select name="shipping_state" id="shipping_state" data-label="State / County"> <option value="">Select an option&hellip;</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> <option value="VT">Vermont</option> </select> <div id="shipping_state_field"></div> <div id="no-ship-state"></div>

You're appending the "shipping_state_field" over and over again.您一遍又一遍地附加“shipping_state_field”。 I'd recommend instead using a div in your markup that you edit whenever necessary instead of adding a new one every time you want the text to appear.我建议改为在您的标记中使用一个 div,您可以在必要时对其进行编辑,而不是每次您希望文本出现时都添加一个新的。 The example below tries to stay as close as possible to your original code:下面的示例尝试尽可能接近您的原始代码:

 var states = ["AL", "IL", "MI", "MS", "UT", "VT", "NH"]; $('#shipping_state').on('change', function() { var state = jQuery("#shipping_state option:selected").text(); jQuery("#no-ship-state").html("Sorry, due to " + state + " state law, alcohol can't be shipped to a " + state + " address"); if (states.includes($(this).val())) { jQuery("#no-ship-state").show(); } else { jQuery("#no-ship-state").hide(); } });
 #no-ship-state { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <select name="shipping_state" id="shipping_state" data-label="State / County"> <option value="">Select an option&hellip;</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> </select> <div id="shipping_state_field"></div> <div id="no-ship-state"></div>

This way .html() is used to replace the content of the div instead of appending an new one every time.这种方式.html()用于替换 div 的内容,而不是每次都附加一个新内容。 Here's some references about the details:以下是一些关于细节的参考:

https://developer.mozilla.org/en-US/docs/Web/API/Element/append https://developer.mozilla.org/en-US/docs/Web/API/Element/append

https://api.jquery.com/html/ https://api.jquery.com/html/

As mentioned in comments, you should't append the DIV each time the user selects from the dropdown, as this creates duplicate IDs.如评论中所述,每次用户从下拉列表中选择时,您不应该 append DIV,因为这会创建重复的 ID。

Create the DIV statically, and just update the name of the state in it.静态创建 DIV,只需更新其中的 state 的名称。 Then hide or show it.然后隐藏或显示它。

 var states = ["AL", "AK", "AZ", "VT"]; $('#shipping_state').on('change', function() { var state = $("#shipping_state option:selected").text(); console.log(state); if (states.includes($(this).val())) { $(".state_name").text(state); jQuery("#no-ship-state").show(); } else { jQuery("#no-ship-state").hide(); } });
 #no-ship-state { display: none; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <select name="shipping_state" id="shipping_state" data-label="State / County"> <option value="">Select an option&hellip;</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> <option value="VT">Vermont</option> <option value="NY">New York</option> </select> <div id="no-ship-state">Sorry, due to <span class="state_name"></span> state law, alcohol can't be shipped to a <span class="state_name"></span> address</div>

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

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