jquery对each和radio操作的示例
时间:2011-06-11 来源:msnbluesky
男 女 我想选中
$(document).ready(function(){
$("#tb tr").each(function(){
//在jquery中用each的时候,经常用到$(this) ,这时如果想查找子项可以通过$(this).find("element")来查找。
$(this).find("input[type=button]:first").click(function(){
alert($(this).attr("id"));
});
});
$(".rSex").each(function(){
$(this).change(function(){
//获取radio选中的值
alert($(".rSex:input[name=rSex]:checked").val());
});
});
$("#txtSelect").blur(function(){
if($.trim($(this).val())!="")
{
if($.trim($(this).val())=='男')
{
//1、设置radio的选中项
$("input[name=rSex]").get(0).checked=true;
//2、设置radio的选中项
//$("input[name=rSex][value='男']").attr("checked",true);
/*3、网上有一种方法是
$("input[name=rSex]").attr("checked","男")
即:attr中是属性checked 和value
我测试是错误的。
*/
}
if($.trim($(this).val())=='女')
{
$("input[name=rSex]").get(1).checked=true;
}
}
});
});
<div>
<input class="rSex" type="radio" value="男" name="rSex" checked="checked" />男
<input class="rSex" type="radio" value="女" name="rSex" />女
我想选中<input type="text" id="txtSelect" value="" />
</div>
<table id="tb">
<tr>
<td>
<input type="button" id="btnOk1" value="确定" />
<input type="button" id="btnCancel1" value="取消" />
</td>
</tr>
<tr>
<td>
<input type="button" id="btnOk2" value="确定" />
<input type="button" id="btnCancel2" value="取消" />
</td>
</tr>
<tr>
<td>
<input type="button" id="btnOk3" value="确定" />
<input type="button" id="btnCancel3" value="取消" />
</td>
</tr>
<tr>
<td>
<input type="button" id="btnOk4" value="确定" />
<input type="button" id="btnCancel4" value="取消" />
</td>
</tr>
<tr>
<td>
<input type="button" id="btnOk5" value="确定" />
<input type="button" id="btnCancel5" value="取消" />
</td>
</tr>
<tr>
<td>
<input type="button" id="btnOk6" value="确定" />
<input type="button" id="btnCancel6" value="取消" />
</td>
</tr>
</table>
相关阅读 更多 +










