如何验证javascript中的选择项目组 [英] How to validate group of select items in javascript

查看:58
本文介绍了如何验证javascript中的选择项目组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< table> 列中,大约有6-7 < select> 项。为了按提交按钮,条件是所有< select> 应该被选中。那么如何使用Javascript来完成这项工作?

There are about 6-7 <select> items in a <table> column. In order to press Submit button, the condition is all <select> items should be selected. So how this can be done using Javascript..?

示例代码看起来像这样:

The sample code seems to be like this:

         <table>
 <tr>
    <td><select name="name1" style="width: 90px; height: 20px"> 
                            <option value = "--" >--</option>
                <option value = "10" >10</option>
         </select>
     </td>
    </tr>
<tr>
    <td><select name="name2" style="width: 90px; height: 20px">
                            <option value = "--" >--</option> 
                <option value = "20" >20</option>
                </select>
     </td>
    </tr>
<tr>
    <td><select name="name3" style="width: 90px; height: 20px"> 
                            <option value = "--" >--</option>
                <option value = "30" >30</option>
                </select>
    </td>
    </tr>
  </table>


推荐答案

<table id="mytable">
  <tr>
    <td>
      <select name="name1" style="width: 90px; height: 20px">
        <option value = "--" >--</option>
        <option value = "10" >10</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      <select name="name2" style="width: 90px; height: 20px">
        <option value = "--" >--</option>
        <option value = "20" >20</option>
     </select>
    </td>
  </tr>
  <tr>
    <td>
      <select name="name3" style="width: 90px; height: 20px">
        <option value = "--" >--</option>
        <option value = "30" >30</option>
      </select>
    </td>
  </tr>
</table>

<form action="" method="post" onsubmit="if(!check()) return false;">
  <input type="submit" />
</form>

<script type="text/javascript">
function check() {
  elements=document.getElementById("mytable").getElementsByTagName("select");
  for(i=0;i<elements.length;i++){
    if(elements[i].value == "--") { return false; }
  }
  return true;
}
</script>

这篇关于如何验证javascript中的选择项目组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆