使用 jquery 禁用 html 表单中提交按钮的功能,但仍使其可见
Disable the function of a submit button in a html form with jquery, but still have it visible
正如你在下面的代码中看到的,我正在进行自动聊天。用户输入文本,代码
响应消息。到目前为止,它工作正常,但现在我想阻止用户在我的消息出现之前发送另一条消息。
假设用户发送了一条消息,之后提交按钮被禁用,从而阻止用户发送更多消息。当代码响应时,按钮再次可用。
我不想隐藏按钮,但我想禁用它的功能。这样它仍然可见,只是在函数runAI运行时不起作用。
如果有人能帮忙,那就太好了。
法典:
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function() {
$("#typing").hide();
var n = "You:<br>";
var o = $('#outputWindow');
var i = $('#inputWindow');
var s = $('#sendButton');
var t = $('#typing');
var r = -1;
//arrays
var msg = ['msg1', 'msg2', 'msg3'];
//fire send events
$(s).click(function() {
runAI();
});
$(i).keydown(function(e) {
if (e.keyCode == 13) {
runAI();
}
});
function runAI() {
if (i.val().length > 0) {
r = r + 1;
o.html(o.html()+n+$("#inputWindow").val()+"<br><hr>" );
setTimeout(function(){ $("#typing").show(); }, 3000);
setTimeout(function(){ o.html(o.html()+"Username:<br>"+msg[r]+"<br><hr>") }, 7000);
setTimeout(function(){ $("#typing").hide(); }, 8000);
if (r+1 >= msg.length)
{
setTimeout(function(){$('#inputWindow').hide(); }, 8000);
setTimeout(function(){$('#sendButton').hide(); }, 8000);
return true; // end the function here;
}
else
{
i.val('');
i.focus();
}
}
}
});
});//]]>
</script>
根据最新的jQuery Docs,最后更新于2015年1月26日。
// Disable
$( "#sendButton" ).prop( "disabled", true );
// Enable
$( "#sendButton" ).prop( "disabled", false );
禁用按钮:
$("#buttonId").prop("disabled",true);
并启用按钮:
$("#buttonId").prop("disabled",false);
我会像这样集成到您的代码中;
$(s).click(function() {
$("#buttonId").prop("disabled",true);
runAI();
});
$(i).keydown(function(e) {
if (e.keyCode == 13) {
$("#buttonId").prop("disabled",true);
runAI();
}
});
然后当runAI()
完成时:
function runAI() {
if (i.val().length > 0) {
r = r + 1;
o.html(o.html()+n+$("#inputWindow").val()+"<br><hr>" );
setTimeout(function(){ $("#typing").show(); }, 3000);
setTimeout(function(){ o.html(o.html()+"Username:<br>"+msg[r]+"<br><hr>") }, 7000);
setTimeout(function(){ $("#typing").hide(); }, 8000);
if (r+1 >= msg.length)
{
setTimeout(function(){$('#inputWindow').hide(); }, 8000);
setTimeout(function(){$('#sendButton').hide(); }, 8000);
return true; // end the function here;
}
else
{
i.val('');
i.focus();
}
$("#buttonId").prop("disabled",false);
}
}
document.getElementById("Submit").disabled = true;
使用:
$("#buttonId").prop("disabled",true);
禁用按钮
$('input[type="submit"], input[type="button"], button').disable(true);
再次启用该按钮
$('input[type="submit"], input[type="button"], button').disable(false);
当然,选择器应该与您的提交按钮匹配。 我的示例将禁用页面上的所有按钮
相关文章:
- 按下一个HTML按钮,该按钮使用一个功能在同一个新窗口中打开URL
- 如何将具有相同功能的两个select html标签的两个JS组合在一起
- 如何将脚本的不同功能放在同一个html页面中
- 使用html 5对元素执行自定义表单验证功能
- 来自“选择”选项的 HTML 画布功能
- 如何使用位置获取滚动功能获取 HTML 中的元素标签
- 使用 jquery 禁用 html 表单中提交按钮的功能,但仍使其可见
- 包括从外部文件到HTML的查询功能(使用Dropbox进行本地测试)
- 将事件处理程序放在HTML按钮上,而不覆盖其默认功能
- 是否可以设置一个功能“;联系我们”;页面使用HTML/CSS
- 在PhpStorm中创建自定义语言,扩展所有html语言功能
- androidphonegap-在单个html页面中导航功能或事件时实现后退按钮
- 倒计时计时器功能和带有html标签的php
- 与 HTML 中的按钮关联的功能 - 未按预期工作
- 具有挖空功能的 HTML 实体
- 如何将此功能实现到我的索引.html
- 如何从 html 的文本字段获取值到 JavaScript 功能
- HTML - PHP 中的“点击”确认功能
- 如何使此功能在我的 html 页面中一次性工作
- HTML <音频>标签的播放功能无法在移动设备上触发