Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
Jquery 对select中的值更改发出警报_Jquery_Html Select - Fatal编程技术网

Jquery 对select中的值更改发出警报

Jquery 对select中的值更改发出警报,jquery,html-select,Jquery,Html Select,我试图在select的值更改为特定值时提醒用户,但我的页面中有多个select(每个用户一个) 以下是我尝试过的: 我希望在select更改为“Chef”值时发出警报 如何执行此操作?您的问题是,使用.text()方法将返回select元素上的所有文本,而不仅仅是“selected”元素,如果您尝试使用.val(),您将使用所选元素的值属性(如果它们没有设置值,则可以使用.val()) 我建议您将if更改为:if($(this).children(':selected').text()==“Ch

我试图在select的值更改为特定值时提醒用户,但我的页面中有多个select(每个用户一个)

以下是我尝试过的:

我希望在select更改为“Chef”值时发出警报


如何执行此操作?

您的问题是,使用
.text()
方法将返回
select
元素上的所有文本,而不仅仅是“selected”元素,如果您尝试使用
.val()
,您将使用所选元素的
属性(如果它们没有设置
,则可以使用
.val()

我建议您将
if
更改为:
if($(this).children(':selected').text()==“Chef”)
,如下所示:

$("select").change(function(){
    if($(this).children(':selected').text() == "Chef")
        {
    alert();
        }
    });
});
if($(this).find('option:selected').text() == "Chef")

演示:

像这样的东西也许:

使用selects时,需要使用.val()函数,而不是.text()函数

此外,还需要与value属性(本例中为1)进行比较,而不是与实际文本值进行比较

代码如下所示:

$("select").change(function(){
    if($(this).children(':selected').text() == "Chef")
        {
    alert();
        }
    });
});
if($(this).find('option:selected').text() == "Chef")
$(文档).ready(函数(){
$(“选择”).change(函数(){
如果($(this.val()=“1”)
{
警报('选择值1(代表厨师)');
}
});
});

您可以这样做:

$("select").change(function(){
    if($(this).children(':selected').text() == "Chef")
        {
    alert();
        }
    });
});
if($(this).find('option:selected').text() == "Chef")