Jquery验证以选中唯一且必需的选择框

时间:2012-05-07 03:31:46

标签: javascript jquery jquery-validate

我使用了jquery validate插件来检查选择框是否唯一且必需。

但是,检查唯一功能 检查所需仅适用于第一个选择框

如何解决这个问题?谢谢

jquery的

        $("#insertResult").validate({
    rules: {
         'select[]': {
              required: true,
              unique: true
          }
       },
   }); 

HTML

<form id="insertResult" method="post" action="excelSQL.php" >
       <select id="selectField0" name="select[]" style="float:left">
                       <option selected="" value="">Select one</option>
    <option value="Email">Email</option>
    <option value="1_Name2">1_Name2</option>
    </select>


    <select id="selectField1" name="select[]" style="float:left">
    <option selected="" value="">Select one</option>
    <option value="Email">Email</option>
    <option value="1_Name2">1_Name2</option>
    </select>
</form>

更新: 我已经编辑了之前完成的js文件,我尝试过selectField0或selectField_0,仍然没有运气

    checkForm: function() {
        this.prepareForm();
        for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
            if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
                for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
                    this.check( this.findByName( elements[i].name )[cnt] );
                }
                } else {
            this.check( elements[i] );
        }
        }
    return this.valid();

},

1 个答案:

答案 0 :(得分:4)

这是jQuery验证器的常见问题。有两种解决此问题的方法:

<强>方法一:

修改jQuery Validator插件,使其按照以下说明处理:http://www.codeboss.in/web-funda/2009/05/27/jquery-validation-for-array-of-input-elements/

<强>方法2:

将Validator应用于每个选择:

HTML:

<form id="insertResult" method="post" action="excelSQL.php" >
    <select id="selectField0" name="select[]" style="float:left" class="error">
        <option selected="" value="">Select one</option>
        <option value="Email">Email</option>
        <option value="1_Name2">1_Name2</option>
    </select>

    <select id="selectField1" name="select[]" style="float:left">
        <option selected="" value="">Select one</option>
        <option value="Email">Email</option>
        <option value="1_Name2">1_Name2</option>
    </select>
    ...
</form>

JavaScript的:

 $("#insertResult").find("select").each(function() {
     $(this).validate({
         rules: {
             'select[]': {
                 required: true,
                 unique: true
              }
         }
     });
 }); 

方法#3:

虽然jQuery插件对于更基本的任务非常有价值,但在某些情况下,您尝试做的事实上可能超出了插件的设计范围。每当我听到像“修改库代码”这样的解决方案时,它就会让我觉得这个库可能不适合我。这取决于修改的复杂性,以及我的同事或继任者在将插件更新到最新版本时可能无法确定原因何时停止工作的可能性。

因此,方法#3完全回避了jQuery验证器,只使用jQuery来促进基本验证:

JavaScript的:

    // submit handler for form
    $('#insertResult').submit(function(event) { 

        // if either field is not selected, show error and don't submit
        if( itemNotSelected( $('#selectField0'), $('#selectField1') ) == true )  { 

            $('.error2').css("display","block");
            event.preventDefault(); 
            return false;

        } else if( isEqual($("#selectField0"), $("#selectField1") ) == true ) {

            $('.error2').css("display","none");
            $('.error1').css("display","block");alert("afda")
            event.preventDefault(); 
            return false;

        } 
    });

    // hide error text on focus of element
    $('#selectField0, #selectField1').focus(function() {
        $('.error2, .error1').hide();

    });

// helper methods to check if both fields are equal
function isEqual(elem1, elem2) {
    return (elem1.find("option:selected").html() == 
            elem2.find("option:selected").html());
}

// helper methods to check if one field (or both) is not selected
function itemNotSelected(elem1, elem2) {
    return ( elem1.find("option:selected").html() == "Select one" || 
             elem2.find("option:selected").html() == "Select one" );
}

HTML:

<select id="selectField0" name="select[]" style="float:left" class="error">
    <option selected="" value="">Select one</option>
    <option value="Email">Email</option>
    <option value="1_Name2">1_Name2</option>
</select>

<select id="selectField1" name="select[]" style="float:left">
    <option selected="" value="">Select one</option>
    <option value="Email">Email</option>
    <option value="1_Name2">1_Name2</option>
</select>
<span class="error2" style="display:none">These are required fields</span>
<span class="error1" style="display:none">Fields must be unique</span>

<input type="submit" name="submit" value="submit" />

当且仅当满足以下条件时,上述代码才会提交表格:

  • 两个选择框都选择了项目。这符合“必要”条件。
  • 选择不同,符合“独特”条件。

  • 当用户关注选择框时,错误消失。

  • 如果用户尝试在上述提交条件为真的情况下提交,则会显示以下错误:

  • 如果未选择一个或多个选择框,则会显示“这些是必填字段”消息。

  • 如果两者相同并且已选中,则会显示“字段必须唯一”消息。
相关问题