lundi 31 octobre 2022

how to get list of checked checkbox value and prepare json object?

<input type="checkbox" name="UserData" id="c1" value="A">
<input type="checkbox" name="UserData" id="c1" value="B">
<input type="checkbox" name="UserData" id="c1" value="C">
<input type="checkbox" name="UserData" id="c1" value="D">

var deleteInfo = "";
var bFirst=true;
$(":checkbox").each(function () {
    var ischecked = $(this).is(":checked");
    if (ischecked) {
        if (bFirst) {
            deleteInfo += '' + $(this).val() + '"';
            bFirst=false;
        } else {
            deleteInfo += ',' + $(this).val() + '"';
        }
    }
});

var strUserID = document.getElementById("sUserID").value;
var myData = {
    "UserID"     : strUserID, 
    deleteInfo
};

$.ajax({
    type: "POST",
    url: "RemoveUserData",
    data: JSON.stringify(myData),
    dataType: "json",
    success: function(result){
        alert (result.status);
    }
});
return false;

i have a jsp page which get user checked value from checkbox and then prepare json array object with user id. but i don't know how to prepare the variable for ajax prcocess. my expected json object is {"UserID": "PETER", "deleteInfo": ["A","C"]}

anyone can help how to format the json value like this: {"UserID": "PETER", "deleteInfo": ["A","C"]}

Thanks.




Aucun commentaire:

Enregistrer un commentaire