默认情况下,我有一个带空白值的下拉列表。当用户选择特定值时,我想显示警报并将下拉列表重置为空值。在下面的示例代码中,我尝试为值3设置条件。如果所选值为3,则显示警报并将下拉列表重置为默认值。
警报显示正常..但以下不起作用
document.getElementById("ddl").selectedIndex = "0";
我能够直到警报,如何在警报后触发操作。我应该使用功能吗?
<html>
<head>
<script>
function jsFunction(value)
{
if (value == 3)
{
alert ("selected value does not match.. Please select the correct one");
document.getElementById("ddl").selectedIndex = "0";
}
}
</script>
</head>
<body>
<select id ="ddl" name="ddl" onchange="jsFunction(this.value);">
<option value=""></option>
<option value='1'>One</option>
<option value='2'>Two</option>
<option value='3'>Three</option>
</select>
</body>
</html>
答案 0 :(得分:1)
baseUriBuilder.scheme("https")
答案 1 :(得分:0)
更改
document.getElementById("ddl").selectedIndex = "1";
到
document.getElementById("ddl").selectedIndex = 0;
答案 2 :(得分:0)
或者你可以这样做:
document.getElementById("ddl").addEventListener('change', function() {
if (this.value == 3) {
alert("selected value does not match.. Please select the correct one");
this.children[0].setAttribute('selected', true);
}
});
<select id="ddl" name="ddl">
<option value=""></option>
<option value='1'>One</option>
<option value='2'>Two</option>
<option value='3'>Three</option>
</select>