繁体   English   中英

如何在特定的jQuery验证结构中验证单选

[英]How to validate radio in specific jquery validation structure

您好,我正在编写一个多步骤表单,并且使用<fieldset> </fieldset>标记将表单的不同部分划分为一个jQuery脚本

我的表单验证在该脚本上运行,看起来像这样

$(".next").click(function(){

  var erros = 0;
    //error checking in the first fieldset
  if (fieldsetn == 1) {
    //verify name
      if ($('input[name="name"]').val().length ===0){
           $('input[name="name"]').attr("placeholder","Please enter your name");
              $('input[name="name"]').addClass('warning');


        erros = 1;
    }
    else {
    $('input[name="name"]').removeClass('warning');
    }
    //verify surname
    if ($('input[name="surname"]').val().length ===0){
         $('input[name="surname"]').attr("placeholder","Please enter your surname");
          $('input[name="surname"]').addClass('warning');

        erros = 1;
    }
    else {
    $('input[name="surname"]').removeClass('warning');
    }

但是我在验证无线电场时遇到问题,我的无线电场像这样设置:

 Chose the Logo pack <a href="services.html"  style="float:right" target="_blank">More Information about Logo Packs Pleas click here</a> 
                        <div id="emotion" name="emotion">
    <input type="radio" name="emotion" id="basi" />
        <label for="basi"><img src="images/basi.jpg" width="630px" alt="I'm sad" /></label><br>

         <input type="radio" name="emotion" id="deli" />
        <label for="deli"><img src="images/deli.jpg" width="630px" alt="I'm sad" /></label><br>

    <input type="radio" name="emotion" id="premi" />
        <label for="premi"><img src="images/premi.jpg" width="630px" alt="I'm happy" /></label>
</div>

如何验证该jquery验证代码结构中的单选字段集,以便必须选择一个选项? 提前致谢

尝试这个-

if($('input[name="emotion"]:checked').length==0)
{
  alert("Please select radio button");
}

要么

if(!$('input[name="emotion"]:checked').length)
{
  alert("Please select radio button");
}

演示版

这会起作用

 if($('input[type="radio"][name="emotion"]').is(':checked')) 
 { 
      alert("it's checked"); 
 }else{
      alert("it's not checked"); 
 }
if($('input[name="emotion"]:checked').length==0)
{
  alert("Please select radio button");
}

这项工作

if(!$('input[name="emotion"]:checked').length)
{
  alert("Please select radio button");
}

暂无
暂无

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

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