admin管理员组文章数量:1425195
I have a checkbox when clicked it fills input form with the value from another input form (e.g. when you have shipping and billing forms and want to save some time by not typing in the same thing twice). So when checked it fills the input, but when unchecked how to delete values?
HTML
$('input[name="shipping"]').click(function() {
$(".billing .country").val($(".shipping .country").val());
$(".billing .city").val($(".shipping .city").val());
});
<script src=".1.1/jquery.min.js"></script>
<input type='checkbox' name='shipping' value='Best Seller' /><label>Same as shipping address</label>
<div class="shipping">
Shipping address
<input class="country" type="text" placeholder="Country">
<input class="city" type="text" placeholder="City">
</div>
<div class="billing">
Billing address
<input class="country" type="text" placeholder="Country">
<input class="city" type="text" placeholder="City">
</div>
I have a checkbox when clicked it fills input form with the value from another input form (e.g. when you have shipping and billing forms and want to save some time by not typing in the same thing twice). So when checked it fills the input, but when unchecked how to delete values?
HTML
$('input[name="shipping"]').click(function() {
$(".billing .country").val($(".shipping .country").val());
$(".billing .city").val($(".shipping .city").val());
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' name='shipping' value='Best Seller' /><label>Same as shipping address</label>
<div class="shipping">
Shipping address
<input class="country" type="text" placeholder="Country">
<input class="city" type="text" placeholder="City">
</div>
<div class="billing">
Billing address
<input class="country" type="text" placeholder="Country">
<input class="city" type="text" placeholder="City">
</div>
JSFiddle
Share Improve this question edited Aug 6, 2018 at 10:27 user3108268 asked Aug 6, 2018 at 10:13 user3108268user3108268 1,0833 gold badges19 silver badges41 bronze badges 1- what is your final solution looks like ? – Niklesh Raut Commented Aug 7, 2018 at 4:45
4 Answers
Reset to default 3Try with checking if checkbox is checked - if not, set empty value:
$('input[name="shipping"]').click(function () {
var checked = $(this).is(':checked');
var value = checked ? $(".shipping .country").val() : '';
$(".billing .country").val(value);
});
$('input[name="shipping"]').click(function () {
if($(this).is(':checked'))
$(".billing .country").val($(".shipping .country").val());
else
$(".billing .country").val('');
});
use an if-else for this to check if checkbox is checked or not
Simply do this
$('input[name="shipping"]').on('change',function(){
var isChecked=$(this).is(":checked"),
value=$(this).val();
$('.country').val(isChecked?value:'');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' name='shipping' value='Best Seller' /><label>Same as shipping address</label>
<div class="shipping">
Shipping address
<input class="country" type="text" placeholder="Country">
</div>
<div class="billing">
Billing address
<input class="country" type="text" placeholder="Country">
</div>
@user3108268 : Use for loop and put name or id or different class to catch that input to copy and then put it for billing section. I am using different name here to distinguish them . like below
$('input[name="shipping"]').click(function() {
var shipping = $(".shipping input");
if($(this).is(":checked")){
for(let i=0;i<shipping.length;i++){
$("[name="+$(shipping[i]).prop("name")+"_2]").val($(shipping[i]).val());
}
}else{
$(".billing input").val("");
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='checkbox' name='shipping' value='Best Seller' /><label>Same as shipping address</label>
<div class="shipping">
Shipping address
<input name="country" class="country" type="text" placeholder="Country">
<input name="city" class="city" type="text" placeholder="City">
</div>
<div class="billing">
Billing address
<input name="country_2" class="country" type="text" placeholder="Country">
<input name="city_2" class="city" type="text" placeholder="City">
</div>
本文标签: javascriptRemove value when checkbox uncheckedStack Overflow
版权声明:本文标题:javascript - Remove value when checkbox unchecked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745374142a2655861.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论