admin管理员组文章数量:1289832
I am working on an autoplete function using JavaScript for a form, and have run into a problem with the checkbox in my HTML file. The basic idea is that the user should be able to fill out their shipping name and zip code in a form fieldset, and then if they check off "Is billing info the same?", a JS function will be called that fills in the billing fieldset for billing name and zip code with the same shipping values. The checkbox is able to acplish this, but I am unable to uncheck it to clear the billing info. Essentially, I cannot uncheck my checkbox. My code is below:
function billingFunction(){
var billName=document.getElementById("billingName");
var shipName=document.getElementById("shippingName");
var billZip=document.getElementById("billingZip");
var shipZip=document.getElementById("shippingZip");
var same=document.getElementById("same");
if (same.checked=true){
billName.value=shipName.value;
billZip.value=shipZip.value;
}
else{
billName.value="";
billZip.value="";
}
}
<form>
<fieldset>
<legend>Shipping Information</legend>
<label for ="shippingName">Name:</label>
<input type = "text" name = "shipName" id = "shippingName" required><br/>
<label for = "shippingZip">Zip code:</label>
<input type = "text" name = "shipZip" id = "shippingZip" pattern = "[0-9]{5}" required><br/>
</fieldset>
<input type="checkbox" id="same" name="same" onchange= "billingFunction()"/>
<label for = "same">Is the Billing Information the Same?</label>
<fieldset>
<legend>Billing Information</legend>
<label for ="billingName">Name:</label>
<input type = "text" name = "billName" id = "billingName" required><br/>
<label for = "billingZip">Zip code:</label>
<input type = "text" name = "billZip" id = "billingZip" pattern = "[0-9]{5}" required><br/>
</fieldset>
<input type = "submit" value = "Verify"/>
</form>
I am working on an autoplete function using JavaScript for a form, and have run into a problem with the checkbox in my HTML file. The basic idea is that the user should be able to fill out their shipping name and zip code in a form fieldset, and then if they check off "Is billing info the same?", a JS function will be called that fills in the billing fieldset for billing name and zip code with the same shipping values. The checkbox is able to acplish this, but I am unable to uncheck it to clear the billing info. Essentially, I cannot uncheck my checkbox. My code is below:
function billingFunction(){
var billName=document.getElementById("billingName");
var shipName=document.getElementById("shippingName");
var billZip=document.getElementById("billingZip");
var shipZip=document.getElementById("shippingZip");
var same=document.getElementById("same");
if (same.checked=true){
billName.value=shipName.value;
billZip.value=shipZip.value;
}
else{
billName.value="";
billZip.value="";
}
}
<form>
<fieldset>
<legend>Shipping Information</legend>
<label for ="shippingName">Name:</label>
<input type = "text" name = "shipName" id = "shippingName" required><br/>
<label for = "shippingZip">Zip code:</label>
<input type = "text" name = "shipZip" id = "shippingZip" pattern = "[0-9]{5}" required><br/>
</fieldset>
<input type="checkbox" id="same" name="same" onchange= "billingFunction()"/>
<label for = "same">Is the Billing Information the Same?</label>
<fieldset>
<legend>Billing Information</legend>
<label for ="billingName">Name:</label>
<input type = "text" name = "billName" id = "billingName" required><br/>
<label for = "billingZip">Zip code:</label>
<input type = "text" name = "billZip" id = "billingZip" pattern = "[0-9]{5}" required><br/>
</fieldset>
<input type = "submit" value = "Verify"/>
</form>
Share
Improve this question
edited Feb 6, 2018 at 21:20
messerbill
5,6391 gold badge31 silver badges39 bronze badges
asked Feb 6, 2018 at 21:00
Matthew RussoMatthew Russo
732 silver badges7 bronze badges
2 Answers
Reset to default 6Your if statement parison is not using the correct equality check, instead it is assigning the checked property as true. Change your code from:
if (same.checked=true){ //This does assignment
billName.value=shipName.value;
billZip.value=shipZip.value;
}
else{
billName.value="";
billZip.value="";
}
To this:
if (same.checked === true){ //This does equality check
billName.value=shipName.value;
billZip.value=shipZip.value;
}
else{
billName.value="";
billZip.value="";
}
You are assigning the value true to same.checked because you have left one equal sign. Modify: if(same.checked == true) or simply if(same.checked)
本文标签: javascriptCannot Uncheck Checkbox (HTML)Stack Overflow
版权声明:本文标题:javascript - Cannot Uncheck Checkbox (HTML) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741483823a2381306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论