正如您在下面的代码中看到的,我正在进行自动聊天。 用户输入文本,代码以消息响应。
到目前为止它工作正常,但现在我想阻止用户在我的消息出现之前发送另一条消息。
因此,假设用户发送了一条消息,之后提交按钮被禁用,阻止用户发送更多消息。当代码响应时,按钮再次可用。
我不想隐藏按钮,但我想禁用它的功能。 这样它仍然是可见的,只是在函数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>
答案 0 :(得分:2)
根据2015年1月26日更新的最新jQuery Docs。
// Disable
$( "#sendButton" ).prop( "disabled", true );
// Enable
$( "#sendButton" ).prop( "disabled", false );
答案 1 :(得分:2)
要禁用按钮:
$("#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);
}
}
答案 2 :(得分:1)
document.getElementById("Submit").disabled = true;
答案 3 :(得分:0)
使用:
$("#buttonId").prop("disabled",true);
答案 4 :(得分:0)
禁用按钮
$('input[type="submit"], input[type="button"], button').disable(true);
再次启用按钮
$('input[type="submit"], input[type="button"], button').disable(false);
当然,选择器应与您的提交按钮匹配。我的示例将禁用页面上的所有按钮