Should jquery preventDefault be used only on anchor tags?
Should prev开发者_如何学运维entDefault
be used only on anchor tags?
E.g do I need to add preventDefault
to a select?
$(".select").onchange(function(e) {
e.preventDefault();
var value = $(this).attr('value');
window.location = '?filter=' + value;
});
You only need to use preventDefault
when there is some default behaviour that you want to prevent.
Changing the value of a select element doesn't have any default behaviour.
Clicking a link does. Submitting a form does too. (This is not an exhaustive list)
You might want to run some JavaScript when a link is clicked, and still follow the link.
There is no hard and fast rule for when you use preventDefault
beyond when there is some default behaviour that you want to prevent.
There is no default action performed when you select something from a select
box..
Form submitting (through the click of a submit button) is something you might want to also intercept with a preventDefault
精彩评论