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. angular.js - SpringMVC+Angular如何設(shè)計路由,從多個頁面不止index.html作為入口2. angular.js - angular中的controller 的js文件如何修改css樣式比如margin:0?3. angular.js - angularjs ng-bind-html如何插入整段HTML4. Android百度地圖怎樣獲取圓的外切矩形5. node.js - vue-cli構(gòu)建報錯。。。生成不了模板,求解~!!6. javascript - 百度echarts圖表如何修改7. javascript - 移動端css動畫播放狀態(tài)暫停在ios不起作用 animation-play-state8. javascript - 微信小程序在wx:for循環(huán)里判斷數(shù)據(jù)再給類名,條件為動態(tài)時無效9. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問題。10. 關(guān)于docker下的nginx壓力測試
