jQuery操作radio和checkbox(赋值取值和绑定事件)

为了兼容ie FF chrome等主流浏览器,对事件部分作了修改:

[b]radio的赋值和事件操作[/b]

// 绑定radio单击方法
$("input[type='radio']").bind("click", function(e) {
var theEvent = window.event || e;
var theObj = theEvent.target || theEvent.srcElement;
var selectedvalue = $(this).val();
if (selectedvalue == "0") {
$('input[name="' + theObj.name + '"][value="1"]').removeAttr("checked");
$('input[name="' + theObj.name + '"][value="0"]').attr("checked",true);
}
else
{
$('input[name="' + theObj.name + '"][value="0"]').removeAttr("checked");
$('input[name="' + theObj.name + '"][value="1"]').attr("checked",true);
}
});


$.ajax(
{
type : 'POST',
url : '../../businessSwitch/getBusinessSwitch',
dataType : 'json',
success : function(msg)
{
// 判断返回结果是否为空
if(isEmpty(msg) || isEmpty(msg[0]))
{
// 提示数据加载失败
alertError();
return;
}

// 查询到的JSON对象
var json = msg[0];

// 赋值
$('input[name=ulMask][value="' + json.ulMask + '"]').attr("checked",true);

$('input[name=ucIMPExtensionCFU][value="' + json.ucIMPExtensionCFU + '"]').attr("checked",true);
$('input[name=ucIMPExtensionCFNA][value="' + json.ucIMPExtensionCFNA + '"]').attr("checked",true);
$('input[name=ucIMPExtensionCFB][value="' + json.ucIMPExtensionCFB + '"]').attr("checked",true);
$('input[name=ucIMPExtensionCFNR][value="' + json.ucIMPExtensionCFNR + '"]').attr("checked",true);
$('input[name=ucIMPExtensionCFNRY][value="' + json.ucIMPExtensionCFNRY + '"]').attr("checked",true);
$('input[name=ucIMPBasicCall][value="' + json.ucIMPBasicCall + '"]').attr("checked",true);
$('input[name=ucIMPAutoSwitchCall][value="' + json.ucIMPAutoSwitchCall + '"]').attr("checked",true);
$('input[name=ucIMPAskSwitchCall][value="' + json.ucIMPAskSwitchCall + '"]').attr("checked",true);
},
error : function(XMLHttpRequest, textStatus, errorThrown)
{
// 提示数据加载失败
alertError();
}


[img]http://dl2.iteye.com/upload/attachment/0094/6866/22aaf279-7eaa-3526-a06f-799348f78feb.jpg[/img]

[b]checkbox的赋值和事件操作[/b]
// 给每一个checkbox绑定方法
$("input[type='checkbox']").bind("click", function(data) {
var theEvent = window.event || e;
var theObj = theEvent.target || theEvent.srcElement;
if (theObj.checked == true) {
$(this).attr("checked",true);

$(this).parent().next().removeClass("invalid_td");
$(this).parent().next().next().children("input").removeClass("invalid_td");
$(this).parent().next().next().children("input").removeAttr("readonly");

if ($(this).parent().next().parent().next().find("input").attr("type") != 'checkbox'
&& $(this).parent().next().parent().next().find("input").attr("name") != 'ucIMPExtensionCoringGroupID') {
$(this).parent().next().parent().next().children().removeClass("invalid_td");
$(this).parent().next().parent().next().children("td").children("input").removeClass("invalid_td");
$(this).parent().next().parent().next().children("td").children("input").removeAttr("readonly");
}
} else {
$(this).attr("checked",false);
if ($(this).parent().next().text() != "") {
$(this).parent().next().addClass("invalid_td");
$(this).parent().next().next().children("input").addClass("invalid_td");
$(this).parent().next().next().children("input").attr("readonly", "readonly");

if ($(this).parent().next().parent().next().find("input").attr("type") != 'checkbox'
&& $(this).parent().next().parent().next().find("input").attr("name") != 'ucIMPExtensionCoringGroupID') {
$(this).parent().next().parent().next().children().addClass("invalid_td");
$(this).parent().next().parent().next().children("td").children("input").addClass("invalid_td");
$(this).parent().next().parent().next().children("td").children("input").attr("readonly", "readonly");
}
}
}
});

$.ajax({
type : 'POST',
url : '../../supplementaryService/getSupplementaryService',
dataType : 'json',
success : function(msg) {
// 判断返回结果是否为空
if (isEmpty(msg) || isEmpty(msg[0])) {
// 提示数据加载失败
alertError();
return;
}

// 查询到的JSON对象
var json = msg[0];
init_value = json;

// 赋值
$('input[name=bIMPExtensionCFUAdmin]').attr("checked", json.bIMPExtensionCFUAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCFNAAdmin]').attr("checked", json.ucIMPExtensionCFNAAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCFBAdmin]').attr("checked",json.ucIMPExtensionCFBAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCFNRAdmin]').attr("checked", json.ucIMPExtensionCFNRAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCFNRYAdmin]').attr("checked",json.ucIMPExtensionCFNRYAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCallWaitAdmin]').attr("checked",json.ucIMPExtensionCallWaitAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCallHoldAdmin]').attr("checked", json.ucIMPExtensionCallHoldAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionDNDAdmin]').attr("checked", json.ucIMPExtensionDNDAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCallerIdAdmin]').attr("checked", json.ucIMPExtensionCallerIdAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionClockAdmin]').attr("checked", json.ucIMPExtensionClockAdmin == '1' ? true : false);
$('input[name=ucIMPExtension3WayConfAdmin]').attr("checked", json.ucIMPExtension3WayConfAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionOipAdmin]').attr("checked", json.ucIMPExtensionOipAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionOirAdmin]').attr("checked", json.ucIMPExtensionOirAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionOiroverAdmin]').attr("checked", json.ucIMPExtensionOiroverAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionPswneedAdmin]').attr("checked", json.ucIMPExtensionPswneedAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionAbdialAdmin]').attr("checked", json.ucIMPExtensionAbdialAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionOutAdmin]').attr("checked", json.ucIMPExtensionOutAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionInAdmin]').attr("checked", json.ucIMPExtensionInAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCbackWhenBusyAdmin]').attr("checked", json.ucIMPExtensionCbackWhenBusyAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionPickupAdmin]').attr("checked", json.ucIMPExtensionPickupAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCoringAdmin]').attr("checked", json.ucIMPExtensionCoringAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionOrringiAdmin]').attr("checked", json.ucIMPExtensionOrringiAdmin == '1' ? true : false);
$('input[name=ucIMPExtensionCycleAdmin]').attr("checked", json.ucIMPExtensionCycleAdmin == '1' ? true : false);
$('input[name=ucIMPAutoSwitchCallAdmin]').attr("checked", json.ucIMPAutoSwitchCallAdmin == '1' ? true : false);
$('input[name=ucIMPAskSwitchCallAdmin]').attr("checked", json.ucIMPAskSwitchCallAdmin == '1' ? true : false);

$('input[name=aucIMPExtensionCFUCode]').val(json.aucIMPExtensionCFUCode);
$('input[name=aucIMPExtensionCFNNYCode]').val(json.aucIMPExtensionCFNNYCode);
$('input[name=aucIMPExtensionCFBCode]').val(json.aucIMPExtensionCFBCode);
$('input[name=aucIMPExtensionCFNRCode]').val(json.aucIMPExtensionCFNRCode);
$('input[name=ucIMPExtensionCfbidAdmin]').val(json.ucIMPExtensionCfbidAdmin);
$('input[name=aucIMPExtensionCFNRCCode]').val(json.aucIMPExtensionCFNRCCode);
$('input[name=aucIMPExtensionCallerIdCode]').val(json.aucIMPExtensionCallerIdCode);
$('input[name=aucIMPExtensionClockTime]').val(json.aucIMPExtensionClockTime);
$('input[name=aucIMPExtensionPswneedCode]').val(json.aucIMPExtensionPswneedCode);
$('input[name=aucIMPExtensionUserCode]').val(json.aucIMPExtensionUserCode);
$('input[name=aucIMPExtensionAbdialCode]').val(json.aucIMPExtensionAbdialCode);
$('input[name=ucIMPExtensionOutCode]').val(json.ucIMPExtensionOutCode);
$('input[name=ucIMPExtensionInCode]').val(json.ucIMPExtensionInCode);
$('input[name=aucIMPExtensionCbackWhenBusyAddr]').val(json.aucIMPExtensionCbackWhenBusyAddr);

$('input[name=ucIMPExtensionCoringGroupID]').val(json.ucIMPExtensionCoringGroupID);
$('input[name=ucIMPExtensionOrringGroupID]').val(json.ucIMPExtensionOrringGroupID);
$('input[name=ucIMPExtensionCycleGroupID]').val(json.ucIMPExtensionCycleGroupID);
// $('input[name=ucIMPAskSwitchCall][value="' + json.ucIMPAskSwitchCall + '"]').attr("checked",true);

// foreach every checkbox, and initialize the td && input css
$("input[type='checkbox']").each(function() {
if ($(this).attr('checked') == 'checked') {
$(this).parent().next().removeClass("invalid_td");
$(this).parent().next().next().children("input")
.removeClass("invalid_td");
$(this).parent().next().next().children("input")
.removeAttr("readonly");

if ($(this).parent().next().parent().next()
.find("input").attr("type") != 'checkbox'
&& $(this).parent().next().parent().next()
.find("input").attr("name") != 'ucIMPExtensionCoringGroupID') {
$(this).parent().next().parent().next().children()
.removeClass("invalid_td");
$(this).parent().next().parent().next()
.children("td").children("input")
.removeClass("invalid_td");
$(this).parent().next().parent().next()
.children("td").children("input")
.removeAttr("readonly");
}
} else {
if ($(this).parent().next().text() != "") {
$(this).parent().next().addClass("invalid_td");
$(this).parent().next().next().children("input")
.addClass("invalid_td");
$(this).parent().next().next().children("input")
.attr("readonly", "readonly");

if ($(this).parent().next().parent().next()
.find("input").attr("type") != 'checkbox'
&& $(this).parent().next().parent().next()
.find("input").attr("name") != 'ucIMPExtensionCoringGroupID') {
$(this).parent().next().parent().next()
.children().addClass("invalid_td");
$(this).parent().next().parent().next()
.children("td").children("input")
.addClass("invalid_td");
$(this).parent().next().parent().next()
.children("td").children("input").attr(
"readonly", "readonly");
}
}
}
});
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
// 提示数据加载失败
alertError();
}
});

[img]http://dl2.iteye.com/upload/attachment/0094/6868/ac6d469d-ee39-34a9-b979-d77ff9a11ce0.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值