admin管理员组文章数量:1302542
I am trying to insert data into the database using ajax
call
my problem is when I click the button twice the data is storing twice
and it is making me sick. How can I avoid it?
<from><button id ="GdStartTest"> </button></form>
$("#GdStartTest").click(function(){
$.ajax({
url: "gdcontroller.php",
method: "POST",
And this controller:
$studentQuery = $conn->query("INSERT INTO r_job_scores
I am trying to insert data into the database using ajax
call
my problem is when I click the button twice the data is storing twice
and it is making me sick. How can I avoid it?
<from><button id ="GdStartTest"> </button></form>
$("#GdStartTest").click(function(){
$.ajax({
url: "gdcontroller.php",
method: "POST",
And this controller:
$studentQuery = $conn->query("INSERT INTO r_job_scores
Share
Improve this question
edited Jan 9, 2017 at 7:24
Suren Srapyan
68.7k14 gold badges125 silver badges117 bronze badges
asked Jan 9, 2017 at 7:23
Mr world wideMr world wide
4,8449 gold badges45 silver badges105 bronze badges
3
-
$("#GdStartTest").click(function(e){e.stopPropagation();... rest of the code
– Death-is-the-real-truth Commented Jan 9, 2017 at 7:26 - Possible duplicate of bind event only once – Death-is-the-real-truth Commented Jan 9, 2017 at 7:28
- Lol re: "making me sick" – Mulan Commented Jan 9, 2017 at 7:31
3 Answers
Reset to default 8Disable the button immediately after clicking it and enable it within the ajax plete callback.
$("#GdStartTest").click(function(){
// cache the button reference
var $this = $(this);
// disable the button
$this.prop('disabled', true);
$.ajax({
url: "gdcontroller.php",
method: "POST",
.........
plete : function(){
// enable the button
$this.prop('disabled', false);
},
......
Or use one()
method to execute the event handler only once.
$("#GdStartTest").one('click', function(){
$.ajax({
url: "gdcontroller.php",
There are so many options to achieve this like, disable the button, put the event listener to off like:
$("#GdStartTest").click(function(){
$(this).prop('disabled', true);
});
$("#GdStartTest").click(function(){
$("#GdStartTest").off();
});
- Disable the button.
Use a Loader on ajax using beforeSend as another parameter.
$.ajax({ beforeSend: function(){ $(".loader").show(); } });
本文标签: javascriptHow to AvoidAjax button click twiceStack Overflow
版权声明:本文标题:javascript - How to Avoid, Ajax button click twice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741703134a2393409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论