我创建 ASP .NET MVC 应用程序。单击按钮时,我调用禁用相同按钮的 JavaScript。 之后(按钮的属性只读被禁用)发布操作不会调用。当我删除禁用按钮的 JavaScript 时,一切都很好。这仅在 Google Chrome 中发生。
当我在 Firefox 或 Internet Explorer 中尝试相同的示例时,一切都很好。
<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>
最佳答案
您需要在禁用按钮之前调用 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>
关于javascript - 禁用 Google Chrome 上的按钮后的 POST 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8157935/