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:默認(rèn)選擇是吧,用jquery的attr就可以了,假設(shè)默認(rèn)選擇值為1的選項,代碼如下:
$('#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}'>)。
所以加一個隱藏的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. mysql - 數(shù)據(jù)庫建表方面的問題?2. javascript - IOS微信audio標(biāo)簽不能通過touchend播放3. javascript - react,獲取radio的值出錯4. javascript - 自執(zhí)行函數(shù)是當(dāng)加載到這個js就執(zhí)行函數(shù)了嗎5. docker不顯示端口映射呢?6. docker鏡像push報錯7. docker start -a dockername 老是卡住,什么情況?8. javascript - web端請求一個正在被修改的文件會怎么樣?9. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?10. angular.js - angular內(nèi)容過長展開收起效果
