javascript 在javascript中验证组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8975749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
validate combobox in javascript
提问by user460920
I have written the below code. But it does not show me the alert message when I don't select the other value and click onto the submit button.
I don't want to use getElementbyId
. I am using the name attribute of the HTML.
我写了下面的代码。但是当我没有选择其他值并单击提交按钮时,它不会向我显示警报消息。我不想使用getElementbyId
. 我正在使用 HTML 的 name 属性。
<HTML>
<HEAD>
<TITLE>ComboBox Validation</TITLE>
<script Language="JavaScript">
function validate()
{
if (document.comboForm.technology.value=="0") \
{
alert("Please Select Technology");
}
}
</script>
</HEAD>
<BODY>
<form name="comboForm">
<select name="technology">
<option value="0">Select</option>
<option value="1">Java Server Pages</option>
</select>
<input type="submit" value="submit" onClick="validate();">
</form>
</BODY>
</HTML>
回答by Adam Rackis
I think you want:
我想你想要:
if (document.forms["comboForm"].technology.value == "0")
But really, stop avoiding document.getElementById
. That's the clearest, easiest way to deal with this:
但真的,不要再逃避了document.getElementById
。这是处理此问题的最清晰、最简单的方法:
<select id="ddTechnology" name="technology">
<option value="0">Select</option>
<option value="1">Java Server Pages</option>
</select>
if (document.getElementById("ddTechnology").value == "0")