javascript javascript下拉验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15368172/
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
javascript dropdown validation
提问by user1794625
I need to alert if you haven't selected anything and couldn't get it to work. It needs to show what is where wrong and alert with a window.
如果你没有选择任何东西并且无法让它工作,我需要提醒。它需要显示什么地方出了问题,并用窗口提醒。
<!DOCTYPE html>
<html>
<head>
<select id="ddlView">
<option value="0">Select</option>
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>
<input type="button" onclick="myFunction()" value="select" />
<script>
function Validate()
{
var e = document.getElementById("ddlView");
var strUser = e.options[e.selectedIndex].value;
var strUser1 = e.options[e.selectedIndex].text;
if(strUser==0)
{
alert("Please select a user");
}
}
</td></head>
</html>
回答by DiverseAndRemote.com
I think you have multiple problems here.
我认为你在这里有很多问题。
- You need to add a body tag
- Set the correct function name in your button.
- you need a closing script tag
- 你需要添加一个body标签
- 在您的按钮中设置正确的功能名称。
- 你需要一个结束脚本标签
Give this a try:
试试这个:
<!DOCTYPE html>
<html>
<body>
<select id="ddlView">
<option value="0">Select</option>
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>
<input type="button" onclick="Validate()" value="select" />
<script type="text/javascript">
function Validate()
{
var e = document.getElementById("ddlView");
var strUser = e.options[e.selectedIndex].value;
var strUser1 = e.options[e.selectedIndex].text;
if(strUser==0)
{
alert("Please select a user");
}
}
</script>
</body>
</html>
回答by Bharath Kumaar
<body>
<div>
<span>Select the course : </span>
<select id="ddlView">
<option value="0">Select</option>
<option value="1">Java Script</option>
<option value="2">CSS</option>
<option value="3">JQuery</option>
<option value="3">HTML</option>
<option value="3">C#</option>
</select>
</div>
<br >
<input type="button" class="btn" value="Submit" id="btnSubmit" onclick="ddlValidate();" />
<script type="text/javascript">
function ddlValidate() {
var e = document.getElementById("ddlView");
var optionSelIndex = e.options[e.selectedIndex].value;
var optionSelectedText = e.options[e.selectedIndex].text;
if (optionSelIndex == 0) {
alert("Please select a Course");
}
else {
alert("Success !! You have selected Course : " + optionSelectedText); ;
}
}
</script>
You can see demo from this link JavaScript dropdown Validation
您可以从此链接查看演示JavaScript 下拉列表验证
回答by pavan kumar
This is not you asked for but you can still consider what i did. Have an optionwith value 0which is going to be the default option of the dropdown and add requiredattribute to the selectelement. Once this selectelement is in a formand is submitted ,HTMLis automatically going to take over the validation part.
这不是你要求的,但你仍然可以考虑我做了什么。有一个值为0的选项,它将成为下拉列表的默认选项,并将required属性添加到select元素。一旦这个select元素在表单中并被提交,HTML将自动接管验证部分。
<select required>
<option value="">Select</option>
<option value="1">Option1</option>
</select>
回答by Pravesh
That has to be in your form processing script, you can just add it to the top of your code and check if it has been submitted.
这必须在您的表单处理脚本中,您只需将其添加到代码的顶部并检查它是否已提交。
// Place this atop of your script.
if(isset($_POST)) {
if($_POST['member_id']) {
// Your codes here
}
}