Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择选项更改时发出警报_Javascript_Html - Fatal编程技术网

Javascript 选择选项更改时发出警报

Javascript 选择选项更改时发出警报,javascript,html,Javascript,Html,我有一个HTML代码,它显示了一个alert()关于选项更改 我如何阻止它在其中一个选项上显示警报,但在所有其他选项上保持警报 <select onchange="alert();"> <option>d</option> <option>de</option> <option>dewe</option> <option>dewee</option> <

我有一个HTML代码,它显示了一个
alert()关于选项更改

我如何阻止它在其中一个选项上显示警报,但在所有其他选项上保持警报

<select onchange="alert();">
    <option>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>

D
判定元件
露水
杜威

这是您不希望选项d引发警报的示例:

 <select onchange="if(this.value!='d'){alert('yes');}">
                <option>d</option>
                <option>de</option>
                <option>dewe</option>
                <option>dewee</option>
            </select>

D
判定元件
露水
杜威

在此onchange中,事件被修改为在触发事件时调用自定义函数。

这是您不希望选项d引发警报的示例:

 <select onchange="if(this.value!='d'){alert('yes');}">
                <option>d</option>
                <option>de</option>
                <option>dewe</option>
                <option>dewee</option>
            </select>

D
判定元件
露水
杜威

在此onchange中,事件被修改为在触发事件时调用自定义函数。

例如,第二个选项
de
不需要警报:

<select onchange="selectChangeHandler(this)">
    <option>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
<script>
    function selectChangeHandler(selectNode) {
         if (selectNode.selectedIndex !== 1) {
             alert("I'm alert option!");
         }
    }
</script>

D
判定元件
露水
杜威
函数selectChangeHandler(selectNode){
如果(selectNode.selectedIndex!==1){
警惕(“我警惕选项!”);
}
}

例如,第二个选项
de
不需要警报:

<select onchange="selectChangeHandler(this)">
    <option>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
<script>
    function selectChangeHandler(selectNode) {
         if (selectNode.selectedIndex !== 1) {
             alert("I'm alert option!");
         }
    }
</script>

D
判定元件
露水
杜威
函数selectChangeHandler(selectNode){
如果(selectNode.selectedIndex!==1){
警惕(“我警惕选项!”);
}
}

您可以简单地创建一个处理程序,并可以这样设置条件

function alertMessage()
{
    if(document.getElementById('select').value !="d"){
        alert('yes');
    }
}
您的标记将是

<select id="select" onchange="alertMessage();">


您可以简单地创建一个处理程序,并可以这样设置条件

function alertMessage()
{
    if(document.getElementById('select').value !="d"){
        alert('yes');
    }
}
您的标记将是

<select id="select" onchange="alertMessage();">

试试这个

<script type="text/javascript">
$(document).ready(function()
{
$("select").change(function()
{
if($(this).val() != "dee")
alert($(this).val());
});
});
</script> 
</head>


<select>
<option>d</option>
<option>de</option>
<option>dee</option>
<option>deep</option>
</select>

$(文档).ready(函数()
{
$(“选择”).change(函数()
{
如果($(this.val()!=“dee”)
警报($(this.val());
});
});
D
判定元件
迪伊
深的
试试这个

<script type="text/javascript">
$(document).ready(function()
{
$("select").change(function()
{
if($(this).val() != "dee")
alert($(this).val());
});
});
</script> 
</head>


<select>
<option>d</option>
<option>de</option>
<option>dee</option>
<option>deep</option>
</select>

$(文档).ready(函数()
{
$(“选择”).change(函数()
{
如果($(this.val()!=“dee”)
警报($(this.val());
});
});
D
判定元件
迪伊
深的

要添加更通用的解决方案,您可以在感兴趣的选项中添加数据属性,请更新
onchange
以调用自定义函数,并将事件数据传递给它,如下所示:

<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];
    if(option.dataset.alert !== undefined){
        alert('Hello Selection');
    }
}
<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];

    // element.dataset is not supported in IE 10 or below
    // use standard attribute syntax instead...
    if (option.getAttribute('data-no-alert') !== null) {
        return;
    }

    alert('Hello Selection');
}
当然,如果您有更多的选项需要提醒,您可以反转逻辑,添加一个数据属性,如
data no alert
,并将代码更改为提醒除具有该属性的选项外的所有选项,如下所示:

<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];
    if(option.dataset.alert !== undefined){
        alert('Hello Selection');
    }
}
<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];

    // element.dataset is not supported in IE 10 or below
    // use standard attribute syntax instead...
    if (option.getAttribute('data-no-alert') !== null) {
        return;
    }

    alert('Hello Selection');
}

-标记警报的特定选项



-将特定选项标记为不警报


IE 10及以下支架的注意事项:

如果需要支持IE 10或更低版本,请将
数据集
查询更改为使用纯旧属性 语法,类似于以下内容:

<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];
    if(option.dataset.alert !== undefined){
        alert('Hello Selection');
    }
}
<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];

    // element.dataset is not supported in IE 10 or below
    // use standard attribute syntax instead...
    if (option.getAttribute('data-no-alert') !== null) {
        return;
    }

    alert('Hello Selection');
}

如果您希望能够使用数据集,也可以使用polyfill数据集使用。

要添加更通用的解决方案,您可以将数据属性添加到感兴趣的选项中,请更新
onchange
以调用自定义函数,并将事件数据传递给它,如下所示:

<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];
    if(option.dataset.alert !== undefined){
        alert('Hello Selection');
    }
}
<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];

    // element.dataset is not supported in IE 10 or below
    // use standard attribute syntax instead...
    if (option.getAttribute('data-no-alert') !== null) {
        return;
    }

    alert('Hello Selection');
}
当然,如果您有更多的选项需要提醒,您可以反转逻辑,添加一个数据属性,如
data no alert
,并将代码更改为提醒除具有该属性的选项外的所有选项,如下所示:

<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];
    if(option.dataset.alert !== undefined){
        alert('Hello Selection');
    }
}
<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];

    // element.dataset is not supported in IE 10 or below
    // use standard attribute syntax instead...
    if (option.getAttribute('data-no-alert') !== null) {
        return;
    }

    alert('Hello Selection');
}

-标记警报的特定选项



-将特定选项标记为不警报


IE 10及以下支架的注意事项:

如果需要支持IE 10或更低版本,请将
数据集
查询更改为使用纯旧属性 语法,类似于以下内容:

<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];
    if(option.dataset.alert !== undefined){
        alert('Hello Selection');
    }
}
<select onchange="doSelectAlert(event);">
    <option data-no-alert>d</option>
    <option>de</option>
    <option>dewe</option>
    <option>dewee</option>
</select>
function doSelectAlert(event) {
    var option = event.srcElement.children[event.srcElement.selectedIndex];

    // element.dataset is not supported in IE 10 or below
    // use standard attribute syntax instead...
    if (option.getAttribute('data-no-alert') !== null) {
        return;
    }

    alert('Hello Selection');
}

如果您希望能够使用数据集,也可以使用polyfill数据集使用。

我相信您必须在handler@NewInTheBusiness这真的不是一条有用的评论,是吗?如果你可以更改html,而不需要其他任何东西,这通常是正确的做法…@user2710234:使用数据属性,您可以创建一个非常通用的解决方案,而无需询问实际选项文本/值本身。我在下面的答案中添加了这方面的例子。我相信你必须在你的测试中测试价值handler@NewInTheBusiness这真的不是一条有用的评论,是吗?如果你可以更改html,而不需要其他任何东西,这通常是正确的做法…@user2710234:使用数据属性,您可以创建一个非常通用的解决方案,而无需询问实际选项文本/值本身。我在下面的答案中添加了这方面的例子。