javascript下拉验证

use*_*625 3 javascript validation

如果您没有选择任何内容并且无法使其工作,我需要提醒您。它需要显示什么地方出了问题,并用窗口提醒。

<!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>
Run Code Online (Sandbox Code Playgroud)

Div*_*com 6

我认为你在这里有很多问题。

  1. 你需要添加一个body标签
  2. 在您的按钮中设置正确的功能名称。
  3. 你需要一个结束脚本标签

试试这个:

<!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>
Run Code Online (Sandbox Code Playgroud)