最近在做需要动态操作html的radio ,按照这样
http://www.java2s.com/Tutorial/JavaScript/0200__Form/Radiochecked.htm
做了,但是却可以把两个按钮同时选中,这样显然不行的,最后利用jquery给解决了,需要的jquery lib请自行到官方下载~
html代码:
<input id="gender" type="radio" name="gender" value="0" />男
<input id="gender" type="radio" name="gender" value="1" />女 *
Js代码:
<% if(get_client_info('gender').to_s=='1') %>
<script type="text/javascript">
$(function() { // set gender from DB
var $radios = $('input:radio[name=gender]');
if($radios.is(':checked') === false) {
$radios.filter('[value=1]').attr('checked', true);
}
});
</script>
<% elsif(get_client_info('gender').to_s =='0') %>
<script>
$(function() {
var $radios = $('input:radio[name=gender]');
if($radios.is(':checked') === false) {
$radios.filter('[value=0]').attr('checked', true);
}
});
</script>
<% end %>
ref:
http://stackoverflow.com/questions/871063/how-to-set-radio-option-checked-onload-with-jquery
posted on 2009-06-16 18:45
fl1429 阅读(2625)
评论(0) 编辑 收藏 所属分类:
Ajax