Javascript 禁用Google Chrome上的按钮后发布不';行不通
我创建ASP.NETMVC应用程序。单击按钮时,我调用禁用同一按钮的JavaScript。 之后(按钮已禁用属性readonly),post操作不会调用。当我删除禁用按钮的JavaScript时,一切都很好。这只发生在谷歌浏览器上 当我在Firefox或Internet Explorer中尝试相同的示例时,一切都很好Javascript 禁用Google Chrome上的按钮后发布不';行不通,javascript,jquery,asp.net-mvc,google-chrome,Javascript,Jquery,Asp.net Mvc,Google Chrome,我创建ASP.NETMVC应用程序。单击按钮时,我调用禁用同一按钮的JavaScript。 之后(按钮已禁用属性readonly),post操作不会调用。当我删除禁用按钮的JavaScript时,一切都很好。这只发生在谷歌浏览器上 当我在Firefox或Internet Explorer中尝试相同的示例时,一切都很好 <script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script&g
<script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
Enable();
});
function Enable() {
$('#btn').removeAttr('disabled');
$('#btn').removeAttr('readonly');
}
function Disable() {
$('#btn').attr('disabled', 'true');
$('#btn').attr('readonly', 'true');
}
</script>
<h2>Example</h2>
<form>
<input id="btn" type="submit" value="StackOverflow" onclick="Disable()" />
</form>
jQuery(文档).ready(函数(){
启用();
});
函数启用(){
$('btn').removeAttr('disabled');
$('#btn').removeAttr('readonly');
}
函数禁用(){
$('#btn').attr('disabled','true');
$('#btn').attr('readonly','true');
}
例子
试试看
编辑
将“输入”类型更改为“按钮”`
$(功能(e){
$(“#btn”)。单击(功能(e){
e、 预防默认值();
$(this.attr('disabled','disabled');//如果可以使用最新版本的jquery并使用.prop(“disabled”,true)
$(“表格”)。提交();
});
});
您需要在禁用按钮之前调用form.submit(),并从事件处理程序返回false
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<script src="//code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
Enable();
});
function Enable() {
$('#btn').removeAttr('disabled');
$('#btn').removeAttr('readonly');
}
function Disable() {
$('#myform').submit();
$('#btn').attr('disabled', 'true');
$('#btn').attr('readonly', 'true');
return false;
}
</script>
<h2>Example</h2>
<form id="myform" method="post">
<input id="btn" type="submit" value="StackOverflow" onclick="return Disable()" />
</form>
</body>
</html>
无标题1
jQuery(文档).ready(函数(){
启用();
});
函数Enable(){
$('btn').removeAttr('disabled');
$('#btn').removeAttr('readonly');
}
函数Disable(){
$('#myform')。提交();
$('#btn').attr('disabled','true');
$('#btn').attr('readonly','true');
返回false;
}
例子
您应该使用$(“…”).prop('disabled',false)代码>对于那些属性(),您正在尝试做什么?防止表单提交两次?@DidierG。OP使用的是jquery 1.4.4
,prop
在1.6或更高版本i中可用think@nachito-是的,我尝试防止表单提交两次。我还尝试包含jQuery 1.7并尝试使用$('…').prop('disabled',false),但没有帮助。没有帮助。功能禁用按钮,但发送到服务器不起作用。当我不调用此函数时,一切正常。我正在尝试多次阻止提交相同的数据。这就是全部。点击后,当POST发生时,提交按钮被启用,用户多次点击该按钮。之后,我们有多个但相同的数据。若按钮在点击后被禁用,因为POST需要20秒,一切都会好起来。这个解决方案还可以,但史蒂文的解决方案对我来说要好一点。再次感谢。
change the `input` type to `button`
<form>
<input id="btn" type="button" value="StackOverflow" />
</form>
$(function(e){
$("#btn").click(function(e){
e.preventDefault();
$(this).attr('disabled', 'disabled'); //if you can use latest version of jquery and use .prop("disabled",true)
$("form").submit();
});
});
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<script src="//code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
Enable();
});
function Enable() {
$('#btn').removeAttr('disabled');
$('#btn').removeAttr('readonly');
}
function Disable() {
$('#myform').submit();
$('#btn').attr('disabled', 'true');
$('#btn').attr('readonly', 'true');
return false;
}
</script>
<h2>Example</h2>
<form id="myform" method="post">
<input id="btn" type="submit" value="StackOverflow" onclick="return Disable()" />
</form>
</body>
</html>