javascript - html中select怎么根據(jù)后臺(tái)傳來的值選中不同的選項(xiàng)值
問題描述
代碼:
<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}的值而顯示不同的選項(xiàng)值,value只有0,1兩個(gè)值。TKS
問題解答
回答1:默認(rèn)選擇是吧,用jquery的attr就可以了,假設(shè)默認(rèn)選擇值為1的選項(xiàng),代碼如下:
$('#class option[value=’1’]').attr(’selected’,true);回答2:
將select標(biāo)簽中的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}'>)。
所以加一個(gè)隱藏的input <input type='hidden' value='{$post.post_class}'/>來獲取后臺(tái)傳來的值,然后再判斷。
<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. thinkPHP5中獲取數(shù)據(jù)庫數(shù)據(jù)后默認(rèn)選中下拉框的值,傳遞到后臺(tái)消失不見。有圖有代碼,希望有人幫忙2. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時(shí)間會(huì)消失是什么情況?3. linux運(yùn)維 - python遠(yuǎn)程控制windows如何實(shí)現(xiàn)4. Python2中code.co_kwonlyargcount的等效寫法5. python - 如何對(duì)列表中的列表進(jìn)行頻率統(tǒng)計(jì)?6. django - Python error: [Errno 99] Cannot assign requested address7. mysql數(shù)據(jù)庫做關(guān)聯(lián)一般用id還是用戶名8. javascript - 如何用最快的速度C#或Python開發(fā)一個(gè)桌面應(yīng)用程序來訪問我的網(wǎng)站?9. python小白,關(guān)于函數(shù)問題10. python小白 關(guān)于類里面的方法獲取變量失敗的問題
