简体   繁体   English

根据单选按钮显示隐藏div和脚本

[英]Show hide div and script based on radio button

I'm trying to implement validation into my form being conditional. 我正在尝试将验证实施为有条件的表单。 Basically if the user selects email radio option then email is going to be required, or if phone is selected then phone field would be required. 基本上,如果用户选择电子邮件单选选项,则将需要电子邮件,或者如果选择了电话,则将需要电话字段。

So far I've gotten this code to work, form submits and validation works fine. 到目前为止,我已经使该代码生效,表单提交和验证工作正常。 But if I switch to phone, then switch back to email, the validation is loaded so form won't submit if I haven't filled out both fields. 但是,如果我切换到电话,然后再切换回电子邮件,则会加载验证,因此如果我没有填写两个字段,则不会提交表单。

I have it set that way but basically trying to make it so if one field is selected then the other required. 我以这种方式设置了它,但基本上是想这样做,所以如果选择一个字段,则需要另一个字段。 Any better ways to do this? 还有更好的方法吗?

HTML: HTML:

<label>Method of Contact</label>
<label class="radio">
    <input type="radio" name="group" value="ck1" checked/>
    Email
</label><br />
<label class="radio"><input type="radio" name="group" value="ck2" />
    Phone
</label>
<div id="emaildisp">
    <label>Email</label> 
    <input id="emailv" name="email" type="email" />
</div>
<div id="phonedisp"> 
    <label>Phone</label> 
    <input id="phonev" name="phone" type="text" />
</div>

Javascript: Javascript:

$(function()
{
    if (jQuery('input[value=ck2]:checked').length > 0)
    {
        jQuery('#phonedisp').show();
        jQuery('#emaildisp').hide();
        jQuery("#phonev").validate(
        {
            expression: "if (VAL) return true; else return false;",
            message: "Please enter your phone number"
        });  
    }
    else 
    {
        jQuery('#phonedisp').hide();
        jQuery('#emaildisp').show();
        jQuery("#emailv").validate(
        {
            expression: "if (VAL) return true; else return false;",
            message: "Please enter your email"
        }); 
    }

    jQuery('input[name=group]').change(function() 
    {
        var selected = jQuery(this).val();console.log(selected);
        if(selected == 'ck2')
        {
            jQuery('#phonedisp').show();
            jQuery('#emaildisp').hide();
            jQuery("#phonev").validate(
            {
                expression: "if (VAL) return true; else return false;",
                message: "Please enter your phone number"
            });  
        } 
        else 
        {
            jQuery('#phonedisp').hide();
            jQuery('#emaildisp').show();
            jQuery("#emailv").validate(
            {
                expression: "if (VAL) return true; else return false;",
                message: "Please enter your email"
            }); 
        }
    }); 
});

Solution: Thanks to the answers below, I came up with the solution. 解决方案:由于以下答案,我提出了解决方案。 Key difference, I was not using jquery validation plugin, rather a different validation script. 关键区别在于,我没有使用jquery验证插件,而是使用了另一个验证脚本。 So I switched over, for beginners, just look it up you'll simply have to add link to the script in the header. 所以我切换了,对于初学者来说,只需查找一下,您只需要在标题中添加指向脚本的链接即可。

Next I gave the form an id, #myform. 接下来,我给表单一个ID,即#myform。 Then I have the ck1 and ck2 radio button their own respective ids, #ck1id and #ck2id. 然后,我有ck1和ck2单选按钮各自的ID#ck1id和#ck2id。 And using the below code, if the radio button is selected, then depending on the id selected, next part becomes validation required. 并使用以下代码,如果选择了单选按钮,则根据选择的ID,下一部分将需要验证。

<script type='text/javascript'>     
$(function(){
jQuery('input[name=group]').change(function() {
  var selected = jQuery(this).val();console.log(selected);
  if(selected == 'ck2'){
    jQuery('#phonedisp').show();
    jQuery('#emaildisp').hide();
    } else {
   jQuery('#phonedisp').hide();
   jQuery('#emaildisp').show();
  }
 }); 
jQuery('input[name=group]').triggerHandler('change');
});
</script>

<script>
$(function(){
    // validate signup form on keyup and submit
    $("#myform").validate({
        rules: {
            group: "required",
            email: 
                {
                    required: '#ck1id:checked',
                    email: true
                },
            phone: 
                {
                    required: '#ck2id:checked',
                    digits: true
                }
        },
        messages: {
            group: "Please select one",
            email: "Please enter your email.",
            phone: "Please enter your phone."
        }
    });
});
</script>

You need to remove the previously added validation from the other fields those are not required. 您需要从其他不需要的字段中删除先前添加的验证。 If you need phone remove validation from email and vice versa. 如果您需要电话,请从电子邮件中删除验证,反之亦然。

You can follow two way, either remove validation or ignore the validation. 您可以采用两种方法,即删除验证或忽略验证。

1. Removing the validation:

    jQuery("#emailv").rules('remove');

            or 

    jQuery("#phonev").rules('remove');

2. Ignore validation:

    jQuery("#emailv").validate({
       ignore: "#emailv"
    });

            or

    jQuery("#phonev").validate({
       ignore: "#phonev"
    });

Check if this helps you. 检查这是否对您有帮助。

Use .rules("remove") to remove jquery validation. 使用.rules("remove")删除jquery验证。

$(function(){
jQuery('input[name=group]').change(function() {
  var selected = jQuery(this).val();console.log(selected);
  if(selected == 'ck2'){
    jQuery('#phonedisp').show();
    jQuery('#emaildisp').hide();
    jQuery("#emailv").rules("remove"); //remove the other field validation
    jQuery("#phonev").validate({
    expression: "if (VAL) return true; else return false;",
    message: "Please enter your phone number"
    });  
  } else {
   jQuery('#phonedisp').hide();
   jQuery('#emaildisp').show();
   jQuery("#phonev").rules("remove"); //remove the other field validation
   jQuery("#emailv").validate({
   expression: "if (VAL) return true; else return false;",
   message: "Please enter your email"
   });  
  }
 }); 
jQuery('input[name=group]').triggerHandler("change");
});

I see that you have duplicated code, just remove it and use jQuery('input[name=group]').triggerHandler("change"); 我看到您有重复的代码,只需删除它并使用jQuery('input[name=group]').triggerHandler("change"); to trigger it when page first loads 在页面首次加载时触发它

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

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