javascript jQuery 检查复选框是否被选中,然后将值添加到输入字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11976476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:56:42  来源:igfitidea点击:

jQuery Check if Checkbox is Checked, then Add Value to Input Field

javascriptjqueryformsinputcheckbox

提问by RevConcept

I have a shopping cart checkout page and I'm trying to add a "gift" option. What needs to happen: Once the checkbox is selected for "Send Order As Gift", a value needs to be assigned to a hidden input field so that the information is moved onto the confirmation page and various receipts.

我有一个购物车结帐页面,我正在尝试添加一个“礼物”选项。需要做什么:一旦选中“将订单作为礼物发送”的复选框,需要为隐藏的输入字段分配一个值,以便将信息移动到确认页面和各种收据上。

HTML:

HTML:

<h3>Send Order As Gift</h3>
<ul>
    <li class="fc_row fc_gift"><label for="gift" class="fc_pre">This is a gift, 
    please do not include a receipt.</label>
    <input type="checkbox" name="gift" id="gift" class="checkbox" value="" />
    <input type="hidden" name="Gift" id="gift-true" value="" /></li>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#gift-true").input( $('#edit-checkbox-id').is(':checked').val() + "Yes"); 
    });?                
    </script>
    </li>
    <li class="fc_row fc_gift_message"><label for="Gift Message">Include a message 
    (limit 100 characters):</label>
    <textarea name="Gift Message" cols="50" rows="3" maxlength="100"></textarea>
    </li>
</ul>

回答by Sebastian vom Meer

You have to update your hidden field whenever the checkbox is changed:

每当更改复选框时,您都必须更新隐藏字段:

$(function(){
    $('#gift').change(function() {
        $("#gift-true").val(($(this).is(':checked')) ? "yes" : "no");
    });
});

回答by nbrooks

Use jQuery .val()to set the value of the input element. jquery .is()returns a boolean value, so you will have to check if it is true(using an ifstatement or a ternary operator like below), then conditionally update the value. Also, you should update this value whenever the box is selected/unselected:

使用jQuery.val()设置输入元素的值。jquery.is()返回一个布尔值,因此您必须检查它是否是true(使用if如下所示的语句或三元运算符),然后有条件地更新该值。此外,无论何时选择/取消选择框,您都应该更新此值:

$(document).ready(function(){
    $('#edit-checkbox-id').change(function() {
        $("#gift-true").val( this.checked ? "Yes" : "" );
    }
});?

Also, if your checkbox is:

此外,如果您的复选框是:

<input type="checkbox" name="gift" id="gift" class="checkbox" value="" />

The appropriate id-selector is $('#gift'), not $('#edit-checkbox-id')

合适的 id-selector 是$('#gift'), 不是$('#edit-checkbox-id')

回答by ShankarSangoli

$('#edit-checkbox-id').is(':checked')returns a boolean value true/false based on checkbox state. So you should use it as a condition and decide what to set the the value using valjQuery method. Try this.

$('#edit-checkbox-id').is(':checked')根据复选框状态返回布尔值真/假。因此,您应该将其用作条件并决定使用valjQuery 方法设置值的内容。试试这个。

$("#gift-true").val( $('#edit-checkbox-id').is(':checked') ? "Yes": "No" ); 

回答by csharp

you can use CSS Pseudo-classe for example :

例如,您可以使用 CSS Pseudo-classe:

var checked=$('input[id="#gift-true"]:checked').length;
var isTrue=false;
if(checked)
    isTrue=true;
else
    isTrue=false;