mardi 30 décembre 2014

How can I pass the value and corresponding name of a checkbox when checked using javascript in ASP.NET?

I'm very new to ajax/javascript so I will try my best to explain my problem. Here's what I have so far:



$(function () {

$("#chkFilter").on("click", "input", function (e)
{
var filterCheckboxes = new Array();
$("#chkFilter").find("input:checked").each(function () {
//console.log($(this).val()); //works fine
filterCheckboxes.push($(this).val());
console.log($(this).val());

//var filterCheckboxes = new Array();
//for (var i = 0; i < e.length; i++) {
// if (e[i].checked)
// filterCheckboxes.push(e[i].value);
//}
});
console.log("calling ajax");
$.ajax({
url: "/tools/oppy/Default.aspx",
type: "post",
dataType: "json",
data: { UpdateQuery: filterCheckboxes }, // using the parameter name
success: function (result) {
if (result.success) {
}
else {
}
}
});
});
});


Every time a checkbox is checked, ajax passes the data onto the server. Here is an example of some checkbox values after a few have been checked in the data form obtained from the Developer's Console:



UpdateQuery%5B%5D=sustrana%3AConditionalQuickWin&UpdateQuery%5B%5D=sustrana%3AQuickWin&UpdateQuery%5B%5D=http%3A%2F%2Fsustrana.com%2Fsustainability%23Environmental



sustrana:ConditionalQuickWin, Sustrana:QuickWin, http://sustrana.com/sustainability#Environmental etc. are the values of the checkboxes. Here, three checkboxes are checked.


But, I also want the name of the checkbox passed, as some checkboxes share the same name like so in this example URL:



tag=http://sustrana.com/sustainability#water&tag=...environment&speed=sustrana:quickwin



Where I can then parse this URL on the server and separate the values that are considered tags and values considered speed. Any and all resources related to this will be extremely appreciated, thank you. Please pardon any misconceptions I have about JS and ajax.





Aucun commentaire:

Enregistrer un commentaire