获取radio的选中值(单选):
var ParentJob = $("input[name='ParentJob'][type='radio']:checked").val();
var Sex = $("input[name='Sex'][checked]").val();
获取checkbox的选中值(多选):
var id_array = new Array();
$('input[name="Job"][type="checkbox"]:checked').each(function () {
id_array.push($(this).val());
});
var Job = id_array.join(',');
设置radio选中:
$(":radio[name='ParentJob'][value='" + item.ParentJob + "']").prop("checked", "checked");
举例:
//回绑班级
var arr = new Array();
arr = classIDs.split(","); //字符分割
for (i = 0; i < arr.length ; i++) {
$(":checkbox[name='class'][value='" + arr[i] + "']").prop("checked", "checked");
}
设置checkbox选中:
$(":checkbox[name='Job'][value='" + item.JobID + "']").prop("checked", "checked");
radio的选择事件
$(function () {
$("input[type=radio][name='keqi']").change(function () { Pin(); });
});
完整例子:
<script type="text/javascript">
$(function () {
//刷新
$("#btnRefresh").click(function () {
window.location.reload();
});
//保存
$("#btnSave").click(function () {
//父岗位
var ParentJob = $("input[name='ParentJob'][type='radio']:checked").val();
if (typeof (ParentJob) == "undefined" || ParentJob == null || ParentJob == "") {
alert("请选择一个父岗位!");
return;
}
//子岗位
var id_array = new Array();
$('input[name="Job"]:checked').each(function () {
id_array.push($(this).val());//向数组中添加元素
});
var Job = id_array.join(',');//将数组元素连接起来以构建一个字符串
if (Job == "" || Job == ',') {
alert("请选择子岗位!");
return;
}
//保存
$.ajax({
url: "/Rights/Job/EditParentJob",
data: { ParentJob: ParentJob, Job: Job, },
success: function (data) {
if (data == "OK") {
alert("编辑成功!");
window.location.reload();
}
else {
alert(data);
}
}
});
});
});
function BindChildJob(id) {
//清除所有子岗位勾选
$("input[name='Job']").removeAttr("checked");
//勾选当前父岗位所包含的子岗位
$.ajax({
url: "/Rights/Job/GetChildJob",
data: { ParentJobID: id, },
success: function (data) {
$.each(data, function (i, item) {
//alert(i);
//alert(item.JobID);
$(":checkbox[name='Job'][value='" + item.JobID + "']").prop("checked", "checked");
});
}
});
}
</script>