使用 onkeyup 调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15661781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:35:19  来源:igfitidea点击:

calling javascript function using onkeyup

javascripthtmlfunctiononkeyup

提问by periback2

I created a function to enable/disable a submit button, and I want it to run when user types something in a text field.

我创建了一个函数来启用/禁用提交按钮,我希望它在用户在文本字段中键入内容时运行。

Here is my code:

这是我的代码:

<form name="sidebarPostMessageForm" action="../app/NewMessageAsynch.action">
    <input type="hidden" name="conversationId" value="${conversationId}"/>
    <lalahtml:textarea id="messageText" name="messageText" onkeyup="enableDisablePostButton();" ></lalahtml:textarea>
    <input type="button" name="postMessage" id="postMessage" onclick="if(this.form.messageText.value.trim().length>0){sidebar_postMessage();this.disabled='disabled'}" value="<ezmi18n:message key="doSendMessage.Label" />"/>
</form>
<script type="text/javascript">
function enableDisablePostButton(){
    if (document.getElementById("messageText").value.trim() == "") {
        document.getElementById("postMessage").disabled="disabled";
    } else {
        document.getElementById("postMessage").disabled="";
    }
}
</script>

When I test it, I get the following javascript error:

当我测试它时,我收到以下 javascript 错误:

Error:

错误:

ReferenceError: enableDisablePostButton is not defined enableDisablePostButton();

ReferenceError: enableDisablePostButton 未定义 enableDisablePostButton();

debugging with firebug

debugging with firebug

It's like if the function enableDisablePostButton() did not exist... Does anyone know why and what can I do?

就像功能 enableDisablePostButton() 不存在一样......有谁知道为什么以及我能做什么?

采纳答案by periback2

I found out the problem, guys! In fact, the javascript block wasn't being loaded because it was being fetched asynchronously (ajax). I found out this now and I set the javascript code in the onkeyup attribute directly:

我发现了问题,伙计们!事实上,javascript 块没有被加载,因为它被异步获取(ajax)。我现在发现了这一点,并直接在 onkeyup 属性中设置了 javascript 代码:

onkeyup="this.form.messageText.value.trim() == ''? this.form.postMessage.disabled='disabled': this.form.postMessage.disabled='';"

thanks for all the help!

感谢所有的帮助!

回答by Parthik Gosar

I think placing the script tag above the form tag should do the trick. The enableDisablePostButton function is defined after the form tag, so it is not able to find the function.

我认为将脚本标签放在表单标签上方应该可以解决问题。enableDisablePostButton 函数是在form标签之后定义的,所以找不到这个函数。

<script type="text/javascript">
function enableDisablePostButton(){
    if (document.getElementById("messageText").value.trim() == "") {
        document.getElementById("postMessage").disabled="disabled";
    } else {
        document.getElementById("postMessage").disabled="";
    }
}
</script>
<form name="sidebarPostMessageForm" action="../app/NewMessageAsynch.action">
    <input type="hidden" name="conversationId" value="${conversationId}"/>
    <lalahtml:textarea id="messageText" name="messageText"     onkeyup="enableDisablePostButton();" ></lalahtml:textarea>
    <input type="button" name="postMessage" id="postMessage" onclick="if(this.form.messageText.value.trim().length>0){sidebar_postMessage();this.disabled='disabled'}" value="<ezmi18n:message key="doSendMessage.Label" />"/>
</form>