如何在特定的jquery验证结构中验证无线电

时间:2014-09-17 09:52:16

标签: javascript jquery html css validation

您好我正在编写一个多步骤表单,并且使用带有<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验证代码结构中验证我的无线电字段集,以便必须选择一个选项?提前谢谢

3 个答案:

答案 0 :(得分:1)

试试这个 -

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

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

Demo

答案 1 :(得分:0)

这将有效

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

答案 2 :(得分:-1)

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

这项工作

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