添加需要选择,但它已经有一个值

时间:2015-04-23 10:33:57

标签: javascript php jquery html5

我想要为#34;品牌汽车"提供一个必需的选择,添加属性但问题是我已经有一个值"选择品牌名称&#34 ; 我该怎么做才能做到这一点? (用于HTML5检查)



$("#category").change(function() {
	var category = parseInt($("#category").val(), 10);
	
	switch (category){
		case 2: //car
			$("#car_part").show("slow"); 
			$("#brand_car").prop('required',true);
		}
		....
	}

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

<select id="brand_car" name="brand_car">
	<option id="0">Choose the brand name</option>
	<option id="10" title="Audi">AUDI</option>
	<option id="16" title="BMW">BMW</option>
</select>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

这个怎么样?

  $("#category").change(function() {
    	var category = parseInt($("#category").val(), 10);
    	
    	switch (category){
    		case 2: //car
    			$("#car_part").show("slow"); 
    			$("#brand_car").prop('required',true);
                break;
    		}
    		
    	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select name="category" id="category">
    	<option value="0">Choose the catecory</option>
    	<option value='2' id='cat2' > Cars </option>
    	<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>
    	<option id="10" title="Audi">AUDI</option>
    	<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>
	<option id="10" title="Audi">AUDI</option>
	<option id="16" title="BMW">BMW</option>
</select>

答案 1 :(得分:0)

您可以为选择添加空值,以便在选择“选择品牌名称”

时需要

代码将是这样的:

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

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