最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how can I check to see if my <select> element contains the multiple attribute - Stack Overflow

programmeradmin2浏览0评论

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:

<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>

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:

<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>
Share Improve this question edited Jul 3, 2012 at 18:15 arrowill12 asked Jul 3, 2012 at 17:57 arrowill12arrowill12 1,8044 gold badges31 silver badges57 bronze badges 3
  • Check this answer stackoverflow.com/questions/1318076/… – Chandu Commented Jul 3, 2012 at 17:59
  • tried that and it is not working skips to my "else statement" – arrowill12 Commented Jul 3, 2012 at 18:10
  • Have posted an answer.. Check it.. – Chandu Commented Jul 3, 2012 at 18:19
Add a comment  | 

5 Answers 5

Reset to default 8

remove : at the beginning of :

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

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

Also simple javascript check:

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:

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

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

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

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

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");
}

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"); 
}

You could try using one of the following boolean indicators that has not been proposed:
let selects = document.getElementsByName('multi_values[]'); // returns collection
Method 1 : hasAttribute()

for (let i = 0; i < selects.length; i++) {
    const select = selects[i];
    if (select.hasAttribute('multiple')) {
        alert('Multiple select found');
        break;
    }
}

Method 2 : bang bang Element.attributes

for (let i = 0; i < selects.length; i++) {
    const select = selects[i];
    if (!!select.attributes['multiple']) {
        alert('Multiple select found');
        break;
    }
}
发布评论

评论列表(0)

  1. 暂无评论