我在 Gridview 中有下拉列表和文本框,因此我想在单击按钮时检查以下内容: (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
关于javascript - 如何使用 JavaScript 检查下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22179045/