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

javascript - How to validate a multiselect Dropdown? - Stack Overflow

programmeradmin1浏览0评论

So the situation is this: I'm creating a dropdown box using the jquery.multiselect plugin on a form, but I need to check if there's some value selected, if there isn't one or more values selected I wanna throw a alert or something when some one tries to submit the form.

The HTML Code

<form id="RNA" name="RNA">
<select size="5" name="sampleMut[]" multiple="multiple" id="sampleMut">

 <option value="41" >41</option>
  <option value="48" >48</option>
  <option value="65" >65</option>
  <option value="102" >102</option>

</select>
</form>

The JavaScript Code

$(function(){
 $("#sampleMut").multiselect(); 
});

jQuery version 1.8.3

jquery.multiselect 1.13

So the situation is this: I'm creating a dropdown box using the jquery.multiselect plugin on a form, but I need to check if there's some value selected, if there isn't one or more values selected I wanna throw a alert or something when some one tries to submit the form.

The HTML Code

<form id="RNA" name="RNA">
<select size="5" name="sampleMut[]" multiple="multiple" id="sampleMut">

 <option value="41" >41</option>
  <option value="48" >48</option>
  <option value="65" >65</option>
  <option value="102" >102</option>

</select>
</form>

The JavaScript Code

$(function(){
 $("#sampleMut").multiselect(); 
});

jQuery version 1.8.3

jquery.multiselect 1.13

Share Improve this question asked Dec 15, 2012 at 13:41 Bruno AndradeBruno Andrade 2613 gold badges8 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

Try this:

$(function(){
    $('form').submit(function(){
         var options = $('#sampleMut > option:selected');
         if(options.length == 0){
             alert('no value selected');
             return false;
         }
    });
});

Fiddle: http://jsfiddle.net/aDxu8/1/

With this you can get the amount of the selected options.

$("#sampleMut option:selected").length;
发布评论

评论列表(0)

  1. 暂无评论