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. docker绑定了nginx端口 外部访问不到2. python - 使用pandas的resample报错3. 网页爬虫 - python 爬取网站 并解析非json内容4. docker-machine添加一个已有的docker主机问题5. html5 - 图片一般一怎么的形式存放在服务器中的?6. python - flask post提交timestamp不能作为参数,这是为什么?7. android - 类似于微信朋友圈图片这样的是在listview中嵌套gridview还是动态加入多个imageview呢?8. vue.js - vue-router开启HTML5的history模式后nginx配置9. html - 类似这样的弹层用什么插件写比较好?10. node.js - mongodb查找子对象的名称为某个值的对象的方法

网公网安备