Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.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 jQuery:所选内容不工作_Javascript_Jquery_Forms - Fatal编程技术网

Javascript jQuery:所选内容不工作

Javascript jQuery:所选内容不工作,javascript,jquery,forms,Javascript,Jquery,Forms,我正在Drupal安装上处理一个表单,并尝试使用jQuery检查是否选中了select选项。但是当我尝试它的时候,结果显示一切都被选中了 我尝试过使用.filter(“:selected”)和just:selected两种方法,但两种方法都得到了相同的结果 下面是我的代码,我还设置了一个来帮助调试 html是: <select> <option value="_none">Select All</option> <option value

我正在Drupal安装上处理一个表单,并尝试使用jQuery检查是否选中了
select选项。但是当我尝试它的时候,结果显示一切都被选中了

我尝试过使用
.filter(“:selected”)
和just
:selected
两种方法,但两种方法都得到了相同的结果

下面是我的代码,我还设置了一个来帮助调试

html是:

<select>
    <option value="_none">Select All</option>
    <option value="225">Residential</option>
    <option value="224">Commercial</option>
</select>
jQuery( "select" ).change(function() {
    if (jQuery ('option[value="225"]').filter(":selected") ) {  // if residential is selected
        console.log('res is selected');
    } else {
        console.log('res is NOT selected');
    }

    if ( jQuery('option[value="224"]').filter(":selected") ) {  // if commercial is selected
        console.log('com is selected');
    } else {
        console.log('com is NOT selected');
    }

    if ( jQuery('option[value="224"]:selected')  || $('option[value="224"]:selected') ) {  // if residential or commercial are selected
        console.log('com or res is selected');
    } else {
        console.log('com or res are NOT selected');
    }
}); 
res is selected
com is selected
com or res is selected
控制台正在记录:

<select>
    <option value="_none">Select All</option>
    <option value="225">Residential</option>
    <option value="224">Commercial</option>
</select>
jQuery( "select" ).change(function() {
    if (jQuery ('option[value="225"]').filter(":selected") ) {  // if residential is selected
        console.log('res is selected');
    } else {
        console.log('res is NOT selected');
    }

    if ( jQuery('option[value="224"]').filter(":selected") ) {  // if commercial is selected
        console.log('com is selected');
    } else {
        console.log('com is NOT selected');
    }

    if ( jQuery('option[value="224"]:selected')  || $('option[value="224"]:selected') ) {  // if residential or commercial are selected
        console.log('com or res is selected');
    } else {
        console.log('com or res are NOT selected');
    }
}); 
res is selected
com is selected
com or res is selected

它们不是布尔结果。检查筛选结果的长度属性或将其更改为使用
is

e、 g


jsiddle:

它们不是布尔结果。检查筛选结果的长度属性或将其更改为使用
is

e、 g


JSFiddle:

对jQuery的调用总是返回一个对象,该对象总是true

您必须检查长度或使用
is

if ( jQuery('option[value="225"]').is(":selected") )
在这种情况下,仅检查select的值似乎更容易

jQuery( "select" ).change(function() {

    switch(this.value) {
        case "255" : // do stuff
        break;

        case "224" : // do stuff
    }
});

对jQuery的调用总是返回一个对象,它总是true

您必须检查长度或使用
is

if ( jQuery('option[value="225"]').is(":selected") )
在这种情况下,仅检查select的值似乎更容易

jQuery( "select" ).change(function() {

    switch(this.value) {
        case "255" : // do stuff
        break;

        case "224" : // do stuff
    }
});

我不认为你的JSFIDLE不起作用。它是给3alerts@Creed布拉顿:这是你的问题。我刚把支票修好。你在逻辑中写了3个警报:)对不起,我瞎了!未在警报中看到“not”一词。谢谢你的帮助:)我不认为你的JSFIDLE不起作用。它是给3alerts@Creed布拉顿:这是你的问题。我刚把支票修好。你在逻辑中写了3个警报:)对不起,我瞎了!未在警报中看到“not”一词。谢谢你的帮助:)