Jquery 如果选中复选框,则不需要电子邮件字段

Jquery 如果选中复选框,则不需要电子邮件字段,jquery,html,Jquery,Html,我有一个带有一些输入字段和一些复选框的表单。通常,用户必须填写电子邮件地址。但如果选中特定复选框,则不需要电子邮件地址 I've made this post earlier: http://stackoverflow.com/questions/39980216/check-if-only-one-checkbox-is-checked/39980734#39980734 But there was not duplicate email required in that question.

我有一个带有一些输入字段和一些复选框的表单。通常,用户必须填写电子邮件地址。但如果选中特定复选框,则不需要电子邮件地址

I've made this post earlier:
http://stackoverflow.com/questions/39980216/check-if-only-one-checkbox-is-checked/39980734#39980734

But there was not duplicate email required in that question. 
But now it has, but only if checkbox `contactpersonen_canseestock_2` is checked, then the email adress is not required.
这是电子邮件的HTML:

<div class="contact-label span2">
  <label for="contactpersonen-email">Email adres</label>
  <div class="contact-input-field">
    <input type="text" class="input-text span2 required contactpersonen-email" id="contactpersonen_email1" name="contactpersonen_email1"></input>
  </div>
</div>
因此,只有:

<label class="checkbox" for="contactpersonen_canseestock_2">Mag goederen afhalen</label>
如果我这样做:

$("#contactpersonen_email1").focusout(function () {
if ($('input:checkbox').is(':checked')) {
       $("input#contactpersonen-email").removeClass( "required" );
}
else{
       $("input#contactpersonen-email").addClass( "required" );
}


});


jQuery.validator.addMethod("contactpersonen-email1",function(value) {




        if ($("#contactpersonen_canseestock_2").is(':checked') || $("#contactpersonen_canseestock_3").is(':checked') || $("#contactpersonen_canseestock_4").is(':checked')
 || $("#contactpersonen_canseestock_5").is(':checked'))    {// && $('#idform :checkbox:checked').length == 1) {



        console.log("checkbox true");
        console.log(contactpersonen_email1.val());
        return true;

        }
        else{
        console.log("checkbox not true");

        return false;

        }
    }, "Email adres bestaat al");

    jQuery.validator.classRuleSettings.checkTotal = { checkTotal: true };

$("#idform").validate();
它不起作用

在文件上方,我有以下内容:

                                contactpersonen_email:{
                                required:true,
                                email:true
                                },
                                contactpersonen_email1:{
                                required:false,
                                email:true
这是我现在拥有的jquery:

jQuery.validator.addMethod("contactpersonen-email1", function(value) {
        if ($("#contactpersonen_canseestock_2").is(':checked')) {
    alert('is checked');
       $("#contactpersonen-email1").removeAttr('required', false);
}
else{
       $("#contactpersonen-email1").Attr('required', true );
}

}, "Please fill in email");




jQuery.validator.addMethod("contactpersonen-email",function(value) {




        if ($("#contactpersonen_canseestock_2").is(':checked') || $("#contactpersonen_canseestock_3").is(':checked') || $("#contactpersonen_canseestock_4").is(':checked')
 || $("#contactpersonen_canseestock_5").is(':checked'))    {// && $('#idform :checkbox:checked').length == 1) {


        console.log("checkbox true");
        console.log(contactpersonen_email1.val());
        return true;

        }
        else{
        console.log("checkbox not true");

        return false;

        }
    }, "Email adres bestaat al");

    jQuery.validator.classRuleSettings.checkTotal = { checkTotal: true };

$("#idform").validate();

                        // Loop through all the input fields for contacts
                        $('#accordion .user-row').each(function (uindex, uvalue) {

         if ($("#contactpersonen_canseestock_2").is(':checked')) {
    console.log('is checked');
        $("#contactpersonen-email1").prop('required',false);
       //$("#contactpersonen-email1").removeAttr('required', false);
}
else{
  //document.getElementById("#contactpersonen-email1").required = true
       //$("#contactpersonen-email1").attr('required', true );
    $("#contactpersonen-email1").prop('required',true);
console.log('is not checked');
}               


                            html += '<tr>';
                            $(this).find('input').each(function (index, value) {                                



                                // Check if input type is a checkbox
                                if ($(this).is(":checkbox")) {
                                    var JaNee = 'Nee';
                                    if ($(this).is(":checked")) JaNee = 'Ja';
                                    html = html + '<td>' + JaNee + '</td>';


                                }
                                else {
                                    // Add the value into the html
                                    html = html + '<td>' + $(this).val() + '</td>';

                                }
                            });

                            html += '</tr>';
                        });

                                    // Set the value from the textarea to the generated html
                                    $('#verploegen_form_klantregistratie_Contactpersonen').val('<table>'+html+'</table>');
                                });

                            });



              $(function () {
                    $("#accordion").accordion({
                        heightStyle: "content"
                    });
                });
这是我的html:

<div class="contact-label span2">
          <label for="contactpersonen-email">Email adres</label>
          <div class="contact-input-field">
            <input type="text" class="input-text span2 required CheckTotal" id="contactpersonen_email1"  name="contactpersonen_email1"></input>
          </div>
        </div>

电子邮件地址
像这样试试

是否要删除“必需”类?请使用以下代码:

if ($('input:checkbox').is(':checked')) {
       $("input#contactpersonen-email").removeClass( "required" );
}
else{
       $("input#contactpersonen-email").addClass( "required" );
}
if ($('input:checkbox').is(':checked')) {
       $("input#contactpersonen-email").removeAttr("required");
}
else{
       $("input#contactpersonen-email").attr( "required" );
}
是否要删除“必需”属性?请使用以下代码:

if ($('input:checkbox').is(':checked')) {
       $("input#contactpersonen-email").removeClass( "required" );
}
else{
       $("input#contactpersonen-email").addClass( "required" );
}
if ($('input:checkbox').is(':checked')) {
       $("input#contactpersonen-email").removeAttr("required");
}
else{
       $("input#contactpersonen-email").attr( "required" );
}

你能为此创建一个工作吗?@SavantCode你到底需要什么?嗨,我用你的建议编辑了这篇文章。但是这个领域仍然需要我们。嗨,谢谢。是的,但我仍然需要外场。我已经添加了孔文件。它很大。但好吧,我想你把这个问题弄得更复杂了。。。首先用更新的内容编辑您的问题。。你刚刚添加了更新
if ($('input:checkbox').is(':checked')) {
       $("input#contactpersonen-email").removeAttr("required");
}
else{
       $("input#contactpersonen-email").attr( "required" );
}