如何检查元素是否包含多个属性

时间:2022-06-07 07:14:12

hi I have tried many options to check if the multiple attribute is set in my select box but none have worked. I am trying to determine if the current select box that I am getting values from is a multiple select so far this is what I have tried:

你好,我尝试了很多选项来检查在我的选择框中是否设置了多重属性,但是没有一个是有效的。我正在尝试确定我正在获取值的当前选择框是否为多重选择,到目前为止这是我尝试过的:

if($(":select[multiple]").length){
           alert("worked");
}

also

if($("select").attr("multiple"){
           alert("worked");
}

also

if($("select").attr("multiple") != 'undefined'{
           alert("worked");
}

html:

html:

<select multiple="multiple" style="height:50px" class="classname" name="multi_values[]"> 
 <option value="blah">blah</option> 
 <option value="blah">blah</option> 
 <option value="blah">blah</option>              
</select>

4 个解决方案

#1


8  

remove : at the beginning of :

删除:在以下开头:

if($("select[multiple]").length){
    alert("worked");
}

Demo : http://jsfiddle.net/D5JX5/

演示:http://jsfiddle.net/D5JX5/

#2


2  

Also simple javascript check:

也简单的javascript检查:

var c = document.getElementsByTagName('select'); //collection
for (var i=0, l = c.length; i<l; i++) {
    alert(typeof c[i].attributes['multiple'] == 'undefined' ? 'single':'multiple');
}

And jQuery equivalent:

与jQuery等价:

$('select').each(function(){
  alert( typeof this.attributes['multiple'] == 'undefined' ? 'single':'multiple' );
});

#3


1  

All the options except ":select[multiple]" (shd be "select[multiple]") you tried should work.

所有选项,除了“:选择[多个]”(shd是“选择[多个]”),您尝试应该工作。

JSFiddle: http://jsfiddle.net/VAXF6/2/

JSFiddle:http://jsfiddle.net/VAXF6/2/

However you are missing a closing paran for your if statement.

但是,如果您的if语句缺少一个结束段。

Change your code to:

改变你的代码:

if($("select[multiple]").length){
           alert("worked");
}

or

if($("select").attr("multiple")){
           alert("worked");
}

or

if($("select").attr("multiple") != 'undefined'){
           alert("worked");
}

Another alternative:

另一个选择:

if($("select").is("[multiple]")){
           alert("worked");
}

#4


1  

It seems you need to only alert if multiple was set with a value, not just if it exists as an attribute:

似乎只有当多个值被设置为一个值时才需要提醒,而不仅仅是当它作为一个属性存在时:

if($("select[multiple='multiple']").length){
    alert("worked"); 
}

#1


8  

remove : at the beginning of :

删除:在以下开头:

if($("select[multiple]").length){
    alert("worked");
}

Demo : http://jsfiddle.net/D5JX5/

演示:http://jsfiddle.net/D5JX5/

#2


2  

Also simple javascript check:

也简单的javascript检查:

var c = document.getElementsByTagName('select'); //collection
for (var i=0, l = c.length; i<l; i++) {
    alert(typeof c[i].attributes['multiple'] == 'undefined' ? 'single':'multiple');
}

And jQuery equivalent:

与jQuery等价:

$('select').each(function(){
  alert( typeof this.attributes['multiple'] == 'undefined' ? 'single':'multiple' );
});

#3


1  

All the options except ":select[multiple]" (shd be "select[multiple]") you tried should work.

所有选项,除了“:选择[多个]”(shd是“选择[多个]”),您尝试应该工作。

JSFiddle: http://jsfiddle.net/VAXF6/2/

JSFiddle:http://jsfiddle.net/VAXF6/2/

However you are missing a closing paran for your if statement.

但是,如果您的if语句缺少一个结束段。

Change your code to:

改变你的代码:

if($("select[multiple]").length){
           alert("worked");
}

or

if($("select").attr("multiple")){
           alert("worked");
}

or

if($("select").attr("multiple") != 'undefined'){
           alert("worked");
}

Another alternative:

另一个选择:

if($("select").is("[multiple]")){
           alert("worked");
}

#4


1  

It seems you need to only alert if multiple was set with a value, not just if it exists as an attribute:

似乎只有当多个值被设置为一个值时才需要提醒,而不仅仅是当它作为一个属性存在时:

if($("select[multiple='multiple']").length){
    alert("worked"); 
}