简体   繁体   English

如何查找是否选中了特定复选框?

[英]How can I find if a specific checkbox is checked?

I've looked at lots of posts but I couldn't find anything that works in my case. 我看了很多帖子,但找不到适合我的情况的任何内容。 I need to display of a preview of the user's signature in their profile, with the options to not show their phone number and/or email address so I have a checkbox for each. 我需要在其个人资料中显示用户签名的预览,并带有不显示其电话号码和/或电子邮件地址的选项,因此我需要为每个复选框都提供一个复选框。

Here's my HTML for the checkboxes: 这是复选框的HTML:

<div class="checkbox">
    <label>
      <input type="checkbox" name="showInSignature[]" id="showPhoneId" value="showPhone" class="checkbox style-0" checked="checked">
      <span>Tel&eacute;fono</span>
    </label>
</div>

<div class="checkbox">
    <label>
      <input type="checkbox" name="showInSignature[]" id="showEmailId" value="showEmail" class="checkbox style-0" checked="checked">
      <span>Direcci&oacute;n de e-mail</span>
    </label>
</div>

and here is the jQuery: 这是jQuery:

var fname = $('#personalOptionsForm').find('[name="fname"]').val(),
    lname = $('#personalOptionsForm').find('[name="lname"]').val(),
    cellphone = $('#personalOptionsForm').find('[name="cellphone"]').val(),
    email = $('#personalOptionsForm').find('[name="email"]').val(),
    if ($('#showPhoneId').is(':checked') = true) {
        showPhone = 1;
    } else {
        showPhone = 0;
    },
    // showPhone = (($('input[name="showInSignature[]"]')['showPhone'].checked = true) ? 1 : 0),
    // showEmail = (($('input[name="showInSignature[]"]')['showEmail'].checked = true) ? 1 : 0),
    str = "<strong>" + fname + " " + lname + "</strong><br /><br />" + ((showPhone = 1) ? 'Tel&eacute;fono: ' + cellphone + '<br />' : '') + "E-mail: <a href=\"mailto:" + email + "\">" + email + "</a>",
    html = $.parseHTML( str );

$('#signaturePreview').append(html);

If I comment out the IF (as I've commented out other tries I've made) and the ternary operator in the str var it works but NOTHING is displayed when trying to use a dynamic value (like the phone checkbox). 如果我注释掉IF(就像我注释掉的其他尝试)和str var中的三元运算符,则它可以工作,但在尝试使用动态值(如电话复选框)时显示NOTHING。 There's only two methods there but I've tried many more. 那里只有两种方法,但我尝试了更多。 None of them work. 他们都不工作。 Nothing is appended. 没有附加任何内容。

How can I do it? 我该怎么做? Thanks in advance! 提前致谢!

showPhone = $('#showPhoneId').is(':checked'); showPhone = $('#showPhoneId')。is(':checked');

surely that's all you need. 当然,这就是您所需要的。 It returns a boolean 它返回一个布尔值

 $(document).ready(function() { alert('Is Checked: ' + $('#showPhoneId')[0].checked); console.log(typeof $('#showPhoneId')[0].checked); console.log($('#showPhoneId')[0].checked === true); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="checkbox"> <label> <input type="checkbox" name="showInSignature[]" id="showPhoneId" value="showPhone" class="checkbox style-0" checked="checked"> <span>Tel&eacute;fono</span> </label> </div> 

In your code you are using assignment operator "=" which means 在您的代码中,您正在使用赋值运算符“ =”,这意味着

a = b (value of b is assigned to a), a = b(将b的值分配给a),

in your case you are trying to assign true to $('#showPhoneId').is(':checked') 在您的情况下,您尝试将true分配给$('#showPhoneId')。is(':checked')

kindly"==" instead 请“ ==”

 var fname = $('#personalOptionsForm').find('[name="fname"]').val() ,
    lname = $('#personalOptionsForm').find('[name="lname"]').val() ,
    cellphone = $('#personalOptionsForm').find('[name="cellphone"]').val(),
    email = $('#personalOptionsForm').find('[name="email"]').val(),

     //dummy values
     fname = 'abc', lname="dksjdn",
     cellphone = "98989898", email = "abc@gmail.com";
    if($('#showPhoneId').is(':checked') == true) {
        showPhone = 1;
    } else {
      showPhone = 0;
    };

    // showPhone = (($('input[name="showInSignature[]"]').is(':checked') == true) ? 1 : 0),
    // showEmail = (($('input[name="showInSignature[]"]').is(':checked') == true) ? 1 : 0),
    str = "<strong>" + fname + " " + lname + "</strong><br /><br />" + ((showPhone == 1) ? 'Tel&eacute;fono: ' + cellphone + '<br />' : '') + "E-mail: <a href=\"mailto:" + email + "\">" + email + "</a>",
    html = $.parseHTML( str );

$('#signaturePreview').append(html);

please refer https://jsbin.com/rolodozode/1/edit?html,js,output 请参考https://jsbin.com/rolodozode/1/edit?html,js,输出

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

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