jQuery验证,以确保至少有一个下拉列表设置为某个值

时间:2012-11-07 07:22:03

标签: javascript jquery html validation

我有一个包含三个下拉列表的表单。确保至少有一个设置为“C”的最简单方法是什么?

带有$(“#Statuses”)的东西。每个或$(“。Droppie”)。每个?

<div id="Statuses">
   <table>
      <tr>
         <td>To Do</td>
         <td>Status</td>
      </tr>
      <tr>
         <td>Call</td>
         <td>
            <select id="Call" name="Call" class="Droppie">
               <option value="A">Active</option>
               <option value="B">Bounced</option>
               <option value="C">Closed</option>
            </select>
         </td>
      </tr>
      <tr>
         <td>Meet</td>
         <td>
            <select id="Meet" name="Meet" class="Droppie">
               <option value="A">Active</option>
               <option value="B">Bounced</option>
               <option value="C">Closed</option>
            </select>
         </td>
      </tr>
      <tr>
         <td>Followup</td>
         <td>
            <select id="Followup" name="Followup" class="Droppie">
               <option value="A">Active</option>
               <option value="B">Bounced</option>
               <option value="C">Closed</option>
            </select>
         </td>
      </tr>
   </table>
   <input type="button" name="Save" id="Save" value="Save" />
</div>

<script>
   SaveCheck = function () {
   -- jquery .each ?? --
     { alert("Sorry, at least one to-do must be closed."); }
   $("#Save").click( SaveCheck );
</script>

2 个答案:

答案 0 :(得分:3)

对于它来说,这是一个更简单的版本:

$('#Save').click(function() {
    if($('.Droppie option[value="C"]:selected').length > 0) {
        alert('successfull');
    }
    else {
        alert("Sorry, at least one to-do must be closed.");
    }
});

答案 1 :(得分:0)

我认为您可以使用此代码

    <div id="Statuses">
   <table>
      <tr>
         <td>To Do</td>
         <td>Status</td>
      </tr>
      <tr>
         <td>Call</td>
         <td>

            <select id="Call" name="Call" id="Call" class="Droppie">
               <option value="A">Active</option>
               <option value="B">Bounced</option>
               <option value="C">Closed</option>
            </select>

         </td>
      </tr>
      <tr>
         <td>Meet</td>
         <td>
            <select id="Meet" name="Meet" id="Meet" class="Droppie" >
               <option value="A">Active</option>
               <option value="B">Bounced</option>
               <option value="C">Closed</option>
            </select >
         </td>
      </tr>
      <tr>
         <td>Followup</td>
         <td>
            <select id="Followup" name="Followup" id="Followup" class="Droppie" >
               <option value="A">Active</option>
               <option value="B">Bounced</option>
               <option value="C">Closed</option>
            </select >
         </td>
      </tr>
   </table>
   <input type="button" name="Save" id="Save" value="Save" onclick="return fnc()"/>
</div>

<script type="text/javascript">
   function fnc()
   {
       call=document.getElementById("Call").value;

       meet=document.getElementById("Meet").value;

       followup=document.getElementById("Followup").value;

       if(call!='C' && meet!='C' && followup!='C' )
       {
           alert("no c is selected");
       return false;
       }
    }
</script>