简体   繁体   中英

value doesn't change when I'm trying to toggling the checkbox

I have this code, when im trying to toggle the checkbox (I can view the changes in inspect), the value of the checkbox doesnt change

 <div class="checkbox checkbox-success"> <input type="checkbox" name="NCONF_RSN1" id="NCONF_RSN1" value=""> <label> Facility is far </label> <input type="hidden" name="NCONF_RSN1" id="NCONF_RSN1" value=""/> </div> 

checkout with value

 if ($(this).prop("checked") == true) {
          $("h2 span").text(checkBoxval);
     }

 $(document).ready(function() { var $checkBox = $("[type='checkbox']"); var checkBoxval = $checkBox.val(); $("h2 span").text(checkBoxval); $checkBox.on("click", function() { if ($(this).prop("checked") == true) { $("h2").show(); $("h2 span").text(checkBoxval); } else { $("h2").hide(); } }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="checkbox checkbox-success"> <input type="checkbox" name="NCONF_RSN1" id="NCONF_RSN1" value="My Checkbox value" checked="true"> <label> Facility is far </label> <input type="hidden" name="NCONF_RSN1" id="NCONF_RSN1" value="" /> <h2>Your value is <span></span></h2> </div> 

好吧,我们看不到你的js代码但是,你不应该像你用NCONF_RSN1那样复制一个id属性

You given the same id to both input element, give two different id. Then it will work

Figured out my problem, added "_" on hidden id

Instead of id="NCONF_RSN1" i used id="NCONF_RSN1_".

i just tried it, i dont know how it worked.

 <div class="checkbox checkbox-success"> <input type="checkbox" name="NCONF_RSN1" id="NCONF_RSN1" value=""> <label> Facility is far </label> <input type="hidden" name="NCONF_RSN1" id="NCONF_RSN1_" value=""/> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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