开发者

How to validate select box based on the other text box value in jquery

开发者 https://www.devze.com 2023-03-29 14:55 出处:网络
I have table like this <table> <tr> <th><select name =\"select1\" >options</select></th>

I have table like this

<table>
  <tr>
    <th><select name ="select1" >options</select></th>
    <th><select name ="select2" >options</select></th>
    <th><select name ="select3" >options</select></th>
    <th><select name ="select4" >options</select></th>
    <th><select name ="select5" >options</select></th>
  </tr>
  <tr>
    <td><input type ="text" name="distance1"></td>
    <td><input type ="text" name="distance2"></td>
    <td><input type ="text" name="distance3"></td>
    <td><input type ="text" name="distance4"></td>
    <td><input type ="text" name="distance5"></td>
  </tr>
  <tr>
    <td><input ty开发者_如何学运维pe ="text" name="distance1"></td>
    <td><input type ="text" name="distance2"></td>
    <td><input type ="text" name="distance3"></td>
    <td><input type ="text" name="distance4"></td>
    <td><input type ="text" name="distance5"></td>
  </tr>
  .
  .
  .
  N rows
</table>

Now I want if the distance1 is filled then a option must be selected from select1 select box, it applies to all the input text boxes.

How can I do this in jQuery?


I supose you have some sort of default option in your select that like

<option value="0">choose value</option>

If that is the case you want to validate when the user presses some button to continue that if the text input has some text then the select must have some option other than 0 selected

Just add this validation to the submit button

var allInputs = $(":input");
var allSelects= $(":select");
for(i = 0;i < allInputs.length; i++)
{
    if(allInputs[i].val() != "" && allSelects[i].val() == 0)
        return false
}

of course you should replace the input and select method if you have other inputs and selects. In that case just add some class and search for that

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号