Jquery loading the link instead of returning false
Here is the jquery code:
$('.edit-function').click( function() {
$.getJSON( $(this).attr('href'), function(data) {
$('input[name=add-name]').val(data.name);
});
return false;
});
});
The problem is every time I run this instead of it updating the value of the form on the current page it goes to the url from t开发者_Go百科he attr. What am I doing wrong?
You have an extra });
in your posted code, it should just be:
$('.edit-function').click( function() {
$.getJSON( $(this).attr('href'), function(data) {
$('input[name=add-name]').val(data.name);
});
return false;
});
Also, make sure you're attaching the click
handler once the element is in the page (and event.preventDefault()
is a better option when you want to just prevent navigation), like this:
$(function() {
$('.edit-function').click(function(e) {
$.getJSON($(this).attr('href'), function(data) {
$('input[name=add-name]').val(data.name);
});
e.preventDefault();
});
});
精彩评论