Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
67
rated 0 times [  70] [ 3]  / answers: 1 / hits: 15921  / 11 Years ago, thu, may 9, 2013, 12:00:00

I have the following HTML form:



<form id=ChartsForm>
<div id=dateoptions>
<p>Until date: <input type=date name=until_date value=Until date></p>
<p>Since date: <input type=date name=since_date value=Since date></p>
</div>
<div class=insightsoptions>
<input id=newLikes class=insightsbuttons type=submit name=submit value=Daily new likes>
<input id=unlikes class=insightsbuttons type=submit name=submit value=Daily unlikes>
</div>
</form>


and the following JQuery script:



$(function () {
$(#newLikes).one('click', function () {
$.ajax({type:'GET', url: 'newLikes.php', data:$('#ChartsForm').serialize(), success:
function(response) {
alert(response);
$(#dailyNewLikes).html(response);
}});
return false;
});
$(#newLikes).on('click', function(){
$(this).toggleClass('green');
$('#dailyNewLikes').toggle();
});


How do I check if the inputs until_date and since_date are empty or not in the first place and if they are empty to stop the script before it executes the ajax call, alert the user about the mistake and then continue if the inputs are not empty anymore. I have to use .one(). I've tried with the .blur() function but with no effect...


More From » jquery

 Answers
43

Instead of using one() you could remove the handler upon success. If you need just the one function removed afterwards you could either use namespaced events (or a named function rather than an anonymous one). You could do something like this:



$(#newLikes).on('click', function () {

var until = $('#dateoptions input[name=until_date]').val();
var since = $('#dateoptions input[name=since_date]').val();

if (until == || since == ) {
alert('Error; until date or since date is missing.');
return;
}

$.ajax({
type:'GET',
url: 'newLikes.php',
data: $('#ChartsForm').serialize(),
success: function(response) {
$(#dailyNewLikes).html(response);
$(#newLikes).off('click');
}
});
});

[#78331] Wednesday, May 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyreese

Total Points: 739
Total Questions: 95
Total Answers: 98

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;