3

如果您没有选择任何内容并且无法使其正常工作,我需要提醒您。它需要显示哪里出了问题并通过窗口提醒。

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

4 回答 4

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>
于 2013-03-12T17:38:27.163 回答
2

这不是你要求的,但你仍然可以考虑我做了什么。有一个值为0的选项,它将成为下拉菜单的默认选项,并将必需的属性添加到select元素。一旦这个select元素在表单中并被提交,HTML将自动接管验证部分。

<select required>
<option value="">Select</option>
<option value="1">Option1</option>
</select>

在此处输入图像描述

于 2018-11-28T07:44:30.773 回答
1
<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>
于 2015-04-22T08:49:53.787 回答
0

这必须在您的表单处理脚本中,您只需将其添加到代码顶部并检查它是否已提交。

// Place this atop of your script.
if(isset($_POST)) {
    if($_POST['member_id']) {
        // Your codes here
    }
}
于 2019-04-05T12:14:39.500 回答