简体   繁体   English

如何使用JavaScript检查下拉值

[英]How to check drop-down values using JavaScript

I have drop-down and Textbox inside a Gridview so I want to check the followings on a button click: (1) Check if NOTHING is selected from the drop down first (the drop-down options are either YES, NO or NA) . 我在Gridview中有一个下拉菜单和Textbox,因此我想单击一下按钮检查以下内容:(1)首先检查是否从下拉菜单中选择NOTHING(下拉选项为YES,NO或NA)。 If nothing is selected, I want to show message that reads like this “Please make selection from the drop-down” (2) If the selection from the drop-down is NO and the Textbox is blank or empty then I want to show message that says: “Please provide comments” 如果未选择任何内容,则要显示如下消息:“请从下拉列表中进行选择”(2)如果下拉列表中的选择为“否”且“文本框”为空白或空,则我想显示消息上面写着:“请提供评论”

The first code checks if the text-box is blank and it works and the 2nd code checks if no selection is made from the drop down and that one works fine too so how can i combine between those 2 codes? 第一个代码检查文本框是否为空白并且可以正常工作,第二个代码检查下拉菜单中是否未选择任何内容,并且也可以正常工作,因此如何在这两个代码之间进行组合? I want to execute both codes on button click, right now it is only calling the first code. 我要在单击按钮时执行这两个代码,现在它仅调用第一个代码。 please help. 请帮忙。 Thanks. 谢谢。 here is my code that checks if the textbox is blank: 这是我的代码,用于检查文本框是否为空:

<script type ="text/javascript">
    function Validate() {
        var flag = false;
        var gridView = document.getElementById('<%= GridView1.ClientID %>');

        for (var i = 1; i < gridView.rows.length; i++) {
            var selects = gridView.rows[i].getElementsByTagName('select');
            var areas = gridView.rows[i].getElementsByTagName('textarea');
            if (selects != null && areas != null) {
                if (areas[0].type == "textarea") {
                    var txtval = areas[0].value;
                    var selectval = selects[0].value;
                    if (selectval == "No" && (txtval == "" || txtval == null)) {

                        flag = false;
                        break;
                    }
                    else {
                        flag = true;
                        document.getElementById('<%=btnSubmit.ClientID%>').style.visibility = 'visible';
                    }
                }
            }
        }
        if (!flag) {
            alert('Please note that comments are required if you select "No" from the dropdown box.  Thanks');
            document.getElementById('<%=btnSubmit.ClientID%>').style.visibility = 'hidden';
        }
        return flag;
    }
</script>

and here is the code that checks the drop-down 这是检查下拉菜单的代码

<script type="text/javascript">
      function validate_DD() {
          var flag = true;
          var dropdowns = new Array(); //Create array to hold all the dropdown lists.
          var gridview = document.getElementById('<%=GridView1.ClientID%>'); //GridView1 is the id of ur gridview.
          dropdowns = gridview.getElementsByTagName('Select'); //Get all dropdown lists contained in GridView1.
          for (var i = 0; i < dropdowns.length; i++) {
              if (dropdowns.item(i).value == 'Select') //If dropdown has no selected value
              {
                  flag = false;
                  break; //break the loop as there is no need to check further.
              }
          }
          if (!flag) {
              alert('Please select either Yes, No or NA in each dropdown and click the Save button again.  Thanks');
              document.getElementById('<%=btnSubmit.ClientID%>').style.visibility = 'hidden';

          }

          return flag;

      }
</script>

Try this: 尝试这个:

<select id="ddlCars">
  <option value="1">Honda</option>
  <option value="2" selected="selected">Toyota</option>
  <option value="3">BMW</option>
</select>

Accessing dropdown: 访问下拉菜单:

To get the value:
var el = document.getElementById("ddlCars");
var val = el.options[el.selectedIndex].value; // val will be 2

To get the text:
var el = document.getElementById("ddlCars");
var car = el.options[el.selectedIndex].text; //car will be Toyota

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM