Dan*_*uva 1 javascript forms jquery
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>
Run Code Online (Sandbox Code Playgroud)
以及以下 JQuery 脚本:
$(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();
});
Run Code Online (Sandbox Code Playgroud)
如何检查输入“until_date”和“since_date”是否为空,以及它们是否为空以在执行 ajax 调用之前停止脚本,提醒用户错误,然后如果输入继续不再是空的了。我必须使用.one(). 我已经尝试过使用 . blur()有功能但没有效果...
one()您可以在成功后删除处理程序,而不是使用它。如果您只需要随后删除一个函数,则可以使用命名空间事件(或命名函数而不是匿名函数)。你可以这样做:
$("#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');
}
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14228 次 |
| 最近记录: |