javascript checkbox and radio selected control and value

Get the selected value of the radio (single choice):

var ParentJob = $("input[name='ParentJob'][type='radio']:checked").val();

Get the selected value of the checkbox (multiple selection):

var id_array = new Array();

$('input[name="Job"][type="checkbox"]:checked').each(function () {
       id_array.push($(this).val());
});

var Job = id_array.join(',');

Set radio selected:

$(":radio[name='ParentJob'][value='" + item.ParentJob + "']").prop("checked", "checked");

Set checkbox checked:

$(":checkbox[name='Job'][value='" + item.JobID + "']").prop("checked", "checked");

Complete example:

<script type="text/javascript">
    $(function () {
        //refresh
        $("#btnRefresh").click(function () {
            window.location.reload();
        });
        //keep
        $("#btnSave").click(function () {
            //parent position
            var ParentJob = $("input[name='ParentJob'][type='radio']:checked").val();
            if (typeof (ParentJob) == "undefined" || ParentJob == null || ParentJob == "") {
                alert("Please select a parent post!");
                return;
            }
            //sub post
            var id_array = new Array();
            $('input[name="Job"]:checked').each(function () {
                id_array.push($(this).val());//Add elements to the array
            });
            var Job = id_array.join(',');//Concatenate array elements to build a string
            if (Job == "" || Job == ',') {
                alert("Please select a sub-post!");
                return;
            }
            //keep
            $.ajax({
                url: "/Rights/Job/EditParentJob",
                data: { ParentJob: ParentJob, Job: Job, },
                success: function (data) {
                    if (data == "OK") {
                        alert("Edited successfully!");
                        window.location.reload();
                    }
                    else {
                        alert(data);
                    }
                }
            });
        });
    });
    function BindChildJob(id) {       
        //Clear all sub-posts checked
        $("input[name='Job']").removeAttr("checked");
        //Check the child positions included in the current parent position
        $.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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484268&siteId=291194637