我正在尝试检测是否从多重选择框中选择了一个值。我的HTML就像

<select id="courses_c" name="courses_c[]" multiple="true" size="6" style="width:150" title="" tabindex="0">
<option label="" value=""></option>
<option label="Diploma of Beauty Therapy - In Class Mode" value="DBTCM" selected="selected">Diploma of Beauty Therapy - In Class Mode</option>
<option label="Diploma of Beauty Therapy - Flexible Delivery" value="DBTFD">Diploma of Beauty Therapy - Flexible Delivery</option>
<option label="Advanced Diploma" value="Advanced_Diploma">Advanced Diploma</option>
<option label="Certificate II in Retail Make-Up and Skin Care" value="CRMS">Certificate II in Retail Make-Up and Skin Care</option>
<option label="Certificate II in Nail Technology" value="CNT">Certificate II in Nail Technology</option>
<option label="Cosmetic Tanning" value="CT">Cosmetic Tanning</option>
<option label="Spray Tanning" value="Spray_Tanning">Spray Tanning</option>
<option label="Design and Apply Makeup Short Course" value="DAMS">Design and Apply Makeup Short Course</option>
<option label="Acrylic and Gels Short Course" value="AGSC">Acrylic and Gels Short Course</option>
<option label="Airbrush Makeup Short Course" value="AMSC">Airbrush Makeup Short Course</option>
<option label="Certificate III in Beauty Services" value="C3BS">Certificate III in Beauty Services</option>
<option label="Certificate IV in Beauty Therapy" value="C4BT">Certificate IV in Beauty Therapy</option>
<option label="Other Short Course" value="OSC">Other Short Course</option>
</select>
<br/>
<br/>
<input type="text" id="primary_course_of_interest_c" />


和我的jQuery就像

$("#courses_c").click(function(){
       var maincourse = $(this).val();
       if(maincourse.contains("DBTCM")) {
            alert("found");
       }
       else {
            alert("not found");
       }
});


不知道为什么contains函数对我不起作用。

最佳答案

Javascript String原型不提供contains方法。请改用match。像下面

$("#courses_c").click(function(){
   var maincourse = $(this).val();
   if(maincourse.match("DBTCM")) {
        alert("found");
   }
   else {
        alert("not found");
   }
});

关于javascript - jQuery包含无法使用multiselect,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32112157/

10-12 06:18