簡體   English   中英

如何使用JavaScript檢查下拉值

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

我在Gridview中有一個下拉菜單和Textbox,因此我想單擊一下按鈕檢查以下內容:(1)首先檢查是否從下拉菜單中選擇NOTHING(下拉選項為YES,NO或NA)。 如果未選擇任何內容,則要顯示如下消息:“請從下拉列表中進行選擇”(2)如果下拉列表中的選擇為“否”且“文本框”為空白或空,則我想顯示消息上面寫着:“請提供評論”

第一個代碼檢查文本框是否為空白並且可以正常工作,第二個代碼檢查下拉菜單中是否未選擇任何內容,並且也可以正常工作,因此如何在這兩個代碼之間進行組合? 我要在單擊按鈕時執行這兩個代碼,現在它僅調用第一個代碼。 請幫忙。 謝謝。 這是我的代碼,用於檢查文本框是否為空:

<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>

這是檢查下拉菜單的代碼

<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>

嘗試這個:

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

訪問下拉菜單:

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