admin管理员组文章数量:1389903
I am trying to prevent user from clicking the submit button more than once. I tried a number of things which are on SO for similar issue.
Like for example :
<script type="text/javascript" src=".11.0.min.js">
$('<% =btnUpload.ClientID %>').click(function () { this.disabled = true });
</script>
I also tried..
<script type="text/javascript" src=".11.0.min.js">
$("form").submit(function() {
$(this).submit(function() {
return false;
});
return true;
});
</script>
and also tried...on master page
<form onsubmit="if(submitted) return false; submitted = true; return true">
But to no use.. I am still able to click on button more than once.
Markup:
<asp:Button ID="btnUpload" runat="server" class="blue-button"
CssClass="ButtonText"OnClick="btnUpload_Click"
OnClientClick="if (!confirm('Are you sure that you want to Save entered details ?'))
return false;return doCustomValidate(event,true);"
Text="Save" />
Also note that I am using master page so
I am trying to prevent user from clicking the submit button more than once. I tried a number of things which are on SO for similar issue.
Like for example :
<script type="text/javascript" src="http://code.jquery./jquery-1.11.0.min.js">
$('<% =btnUpload.ClientID %>').click(function () { this.disabled = true });
</script>
I also tried..
<script type="text/javascript" src="http://code.jquery./jquery-1.11.0.min.js">
$("form").submit(function() {
$(this).submit(function() {
return false;
});
return true;
});
</script>
and also tried...on master page
<form onsubmit="if(submitted) return false; submitted = true; return true">
But to no use.. I am still able to click on button more than once.
Markup:
<asp:Button ID="btnUpload" runat="server" class="blue-button"
CssClass="ButtonText"OnClick="btnUpload_Click"
OnClientClick="if (!confirm('Are you sure that you want to Save entered details ?'))
return false;return doCustomValidate(event,true);"
Text="Save" />
Also note that I am using master page so
Share Improve this question edited Apr 2, 2014 at 7:03 Rohan Kumar 40.6k11 gold badges80 silver badges110 bronze badges asked Apr 2, 2014 at 6:44 SamuraiJackSamuraiJack 5,56918 gold badges103 silver badges212 bronze badges 3- Once the form submits the page probably reloads, and you start over again, all javascript is lost. – adeneo Commented Apr 2, 2014 at 6:49
-
not related, but for ease of JS/jQuery set your button control ClientIDMode to "Static" then in the jQuery you can use standard referencing :
$('#btnUpload')
– fnostro Commented Apr 2, 2014 at 6:56 - why dont you disable in code behind (.cs file) logic like button clicked event – Ajay Commented Apr 2, 2014 at 9:21
4 Answers
Reset to default 2If you want the user to click on the button just once, use .one()
to attach event.
jquery url : http://api.jquery./one/
Add a disabled attribute to it.
$('#<% =btnUpload.ClientID %>').click(function () {
$(this).prop("disabled", true);
});
You missed #
in your id
prepend it before <% =btnUpload.ClientID %>
like,
$(function(){
$('#<% =btnUpload.ClientID %>').click(function () {
this.disabled = true;
});
});
If you want to add click event once only then use one() like,
$(function(){
$('#<% =btnUpload.ClientID %>').one('click',function () {
// your code on click event
});
});
If, your id
changes(Clientid changes
after render
) then try to use class selector
like,
$(function(){
$('.ButtonText').click(function () {
this.disabled = true;
});
});
Set one count
variable in javascript. initialize it's value as 0
. In .click()
event listener check the condition. If
it is 0
, allow to execute the code and increase count by 1
. Else
, return false.
var count = 0;
$('#<% =btnUpload.ClientID %>').click(function () {
if (count == 0) {
count++;
return true;
} else {
return false;
}
});
本文标签: javascriptHow to prevent user from clicking button more than onceStack Overflow
版权声明:本文标题:javascript - How to prevent user from clicking button more than once? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744629896a2616474.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论