javascript - html中select怎么根據(jù)后臺傳來的值選中不同的選項值
問題描述
代碼:
<tr> <th>空間性質(zhì)</th> <td> <input type='hidden' value='{$post.post_class}'/> <select name='post[post_class]' value='{$post.post_class}'> <option value='0' id='op1'>出售</option> <option value='1' id='op2'>出租</option> </select> </td> </tr>
根據(jù)value={$post.post_class}的值而顯示不同的選項值,value只有0,1兩個值。TKS
問題解答
回答1:默認選擇是吧,用jquery的attr就可以了,假設默認選擇值為1的選項,代碼如下:
$('#class option[value=’1’]').attr(’selected’,true);回答2:
將select標簽中的value置為0 或 1 不就可以了嗎
回答3:$('#class option[value=’1’]').attr(’selected’,true);或$('#class').val(1);回答4:
http://jsrun.net/d9YKp
回答5:由于:document.querySelector(’#class’).value獲取不到select中的value值(即<select name='post[post_class]' value='{$post.post_class}'>)。
所以加一個隱藏的input <input type='hidden' value='{$post.post_class}'/>來獲取后臺傳來的值,然后再判斷。
<script type='text/javascript'> var sv = document.getElementById(’class’).value; if(sv == 0){$('#class2 option[value=’0’]').attr(’selected’,true); }else {$('#class2 option[value=’1’]').attr(’selected’,true); }</script>
相關(guān)文章:
1. python - pymysql建立連接出錯2. 默認輸出類型為json,如何輸出html3. mysql 遠程連接出錯10060,我已經(jīng)設置了任意主機了。。。4. python的正則怎么同時匹配兩個不同結(jié)果?5. 數(shù)組排序,并把排序后的值存入到新數(shù)組中6. php多任務倒計時求助7. MySQL的聯(lián)合查詢[union]有什么實際的用處8. PHP訂單派單系統(tǒng)9. html5 - css3scale和rotate同時使用轉(zhuǎn)換成matrix寫法該如何轉(zhuǎn)換?10. win10 python3.5 matplotlib使用報錯
