javascript - html中select怎么根據后臺傳來的值選中不同的選項值
問題描述

代碼:
<tr> <th>空間性質</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>
根據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>
相關文章:
1. boot2docker無法啟動2. docker-compose中volumes的問題3. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””4. docker容器呢SSH為什么連不通呢?5. java - SSH框架中寫分頁時service層中不能注入分頁類6. dockerfile - 為什么docker容器啟動不了?7. 關于docker下的nginx壓力測試8. node.js - antdesign怎么集合react-redux對input控件進行初始化賦值9. docker安裝后出現Cannot connect to the Docker daemon.10. nignx - docker內nginx 80端口被占用

網公網安備