jQuery包含不使用multiselect

时间:2015-08-20 07:34:07

标签: javascript jquery

我正在尝试检测是否存在从多选框中选择的值。我的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功能对我不起作用。

1 个答案:

答案 0 :(得分:1)

Javascript String prototypent提供contains方法。请改用match。如下所示

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