(JQuery) How to call a custom function before following link

You might want to see my answer to a similar question. Basically, an ajax post would be asynchronous by default and may not make it to the server before completing. In order to ensure completion, you can make it synchronous but this will lock up the browser until the request has returned. This can be very problematic if your server is having trouble, it could take a while and the user is waiting to follow the link. Nevertheless, the code is:
$('#hotlink').click( function () { 
$
.ajax({
async
: false,
type
: "POST",
url
: "http://myurl.com/mypage.php",
data
: "param1=value&param2=value", // &param3=value... etc.
});
});
Upon completion, the default action of the link will be honoured.

MVC – Postback on clicking a radio button

If you had radio buttons like:
 type="radio" name="sex" value="male" /> Male
/>
type="radio" name="sex" value="female" /> Female
You could add script (assuming you can use jQuery) on the page like:
 type="text/javascript">
$
(function () {
$
(':radio[name="sex"]').change(function () {
$
.ajax({
url
: 'sex',
type
: 'POST',
data
: { sex: $(':radio[name="sex"]:checked').val() },
success
: function (xhr_data) {
alert
(xhr_data.someValue);
}
});
});
});
Assuming you have an action method in the same controller as the one that generated your view:
public class YourController : Controller
{
public ActionResult sex(string sex)
{
// do something awesome
return Json(new { someValue = "testing!" });
}
}
share|improve this answer
http://stackoverflow.com/questions/6916322/mvc-postback-on-clicking-a-radio-button

How to loop through array in jquery

Three options:

Generic loop:

var i;
for (i = 0; i < substr.length; ++i) {
// do something with `substr[i]`
}
Advantages: Straight-forward, no dependency on jQuery, easy to understand, no issues with preserving the meaning of this within the body of the loop, no unnecessary overhead of function calls (e.g., in theory faster, though in fact you’d have to have so many elements that the odds are you’d have other problems; details).

ES5’s forEach:

As of ECMAScript5, arrays have a forEach function on them which makes it easy to loop through the array:
substr.forEach(function(item) {
// do something with `item`
});
Advantages: Declarative, can use a prebuilt function for the iterator if you have one handy, if your loop body is complex the scoping of a function call is sometimes useful, no need for an i variable in your containing scope.
Disadvantages: Not all browsers have it yet, although most do, and you can always use an ES5 shim (a search will list several) to provide it on browsers that don’t have it yet. If you’re using this in the containing code, you have to stick it in a variable so you can use it within the function or pass it as a second argument to forEach, since within the iteration function, this will be undefined (in strict mode) or the global object (window) in non-strict mode unless you give forEach a specific value for it.

jQuery.each:

jQuery.each(substr, function(index, item) {
// do something with `item` (or `this` is also `item` if you like)
});
Advantages: All of the same advantages as forEach, plus you know it’s there since you’re using jQuery.
Disadvantages: If you’re using this in the containing code, you have to stick it in a variable so you can use it within the function, since this means something else within the function.

What NOT to do:

Don’t use for..in for this (or if you do, do it with proper safeguards). You’ll see people saying to (in fact, briefly there was an answer here saying that), but for..in does not do what many people think it does (it does something even more useful!). Specifically, for..in loops through the enumerable property names of an object (not the indexes of an array). Since arrays are objects, and their only enumerable properties by default are the indexes, it mostly seems to sort of work in a bland deployment. But it’s not a safe assumption that you can just use it for that. Here’s an exploration: http://jsbin.com/exohi/3
I should soften the “don’t” above. If you’re dealing with sparse arrays (e.g., the array has 15 elements in total but their indexes are strewn across the range 0 to 150,000 for some reason, and so the length is 150,001), and if you use appropriate safeguards like hasOwnProperty and checking the property name is really numeric (see link above), for..in can be a perfectly reasonable way to avoid lots of unnecessary loops, since only the populated indexes will be enumerated.