2015-04-23 46 views
0

我想有一個請求選擇「品牌汽車」,該屬性被添加,但問題是我已經有一個值「選擇品牌名稱」 我該如何做這項工作? (對於HTML5檢查)添加需要選擇,但它已經有一個值

$("#category").change(function() { 
 
\t var category = parseInt($("#category").val(), 10); 
 
\t 
 
\t switch (category){ 
 
\t \t case 2: //car 
 
\t \t \t $("#car_part").show("slow"); 
 
\t \t \t $("#brand_car").prop('required',true); 
 
\t \t } 
 
\t \t .... 
 
\t }
<select name="category" id="category"> 
 
\t <option value="0">Choose the catecory</option> 
 
\t <option value='2' id='cat2' > Cars </option> 
 
\t <option value='88' id='cat88' > Trucks </option> 
 
</select> 
 

 
<select id="brand_car" name="brand_car"> 
 
\t <option id="0">Choose the brand name</option> 
 
\t <option id="10" title="Audi">AUDI</option> 
 
\t <option id="16" title="BMW">BMW</option> 
 
</select>

+0

你是什麼意思'我怎麼能做這個工作' – ozil

回答

0

你可以,如果他選擇「選擇名牌」

代碼會是這樣的空值添加到選擇,因此將需要:

<select name="category" id="category"> 
 
\t <option value="0">Choose the catecory</option> 
 
\t <option value='2' id='cat2' > Cars </option> 
 
\t <option value='88' id='cat88' > Trucks </option> 
 
</select> 
 

 
<select id="brand_car" name="brand_car"> 
 
\t 
 
    <option value="" >Choose the brand name</option> 
 
\t <option id="10" title="Audi">AUDI</option> 
 
\t <option id="16" title="BMW">BMW</option> 
 
</select>

+0

感謝它的工作!:) – Eagle

0

這個怎麼樣?

$("#category").change(function() { 
 
    \t var category = parseInt($("#category").val(), 10); 
 
    \t 
 
    \t switch (category){ 
 
    \t \t case 2: //car 
 
    \t \t \t $("#car_part").show("slow"); 
 
    \t \t \t $("#brand_car").prop('required',true); 
 
       break; 
 
    \t \t } 
 
    \t \t 
 
    \t });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<select name="category" id="category"> 
 
    \t <option value="0">Choose the catecory</option> 
 
    \t <option value='2' id='cat2' > Cars </option> 
 
    \t <option value='88' id='cat88' > Trucks </option> 
 
    </select> 
 

 
    <select id="brand_car" name="brand_car"> 
 
     <option selected="true" style="display:none;">Choose the brand name</option> 
 
    \t <option id="10" title="Audi">AUDI</option> 
 
    \t <option id="16" title="BMW">BMW</option> 
 
    </select> 
 

 

 
<!--If you want to display default text as option but want to disable it below will work--> 
 

 
<select id="brand_car" name="brand_car"> 
 
     <option selected disabled>Choose the brand name</option>name</option> 
 
\t <option id="10" title="Audi">AUDI</option> 
 
\t <option id="16" title="BMW">BMW</option> 
 
</select>

相關問題