16.4 验证列表框中的值是否重复.htm

来自「这是一些常用的JavaScript的特效的源码和教程」· HTM 代码 · 共 30 行

HTM
30
字号
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>标题页</title>
<script language="javascript">
function chk()
{
    var txtValue = document.getElementById("txtValue");//获取文本框的值
    var sel = document.getElementById("sel");          //找到下拉框
    for (var i=0; i<sel.length; i++)                    //遍历下拉框中的所有数据
    {
        if (txtValue.value==sel.options[i].text){       //如果输入的值等于下拉框中的值
            alert("该值已经存在");
        }
    }
}
</script>
</head>
<body>
<div id="mydiv">
<select id="sel" name="sel">
<option>China</option>
<option>England</option>
<option>German</option>
</select>
<input type="text" name="txtValue" id="txtValue"><br/>
<input type="button" value="检测是否存在" onclick="chk();">
</div>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?