onsubmit 函数没有被调用

发布于 2024-12-08 20:09:13 字数 1053 浏览 0 评论 0原文

我正在设计一个基于AJAX 的shoutbox。 http://businessgame.be/shoutbox.php

该脚本在 google chrome 中完美运行,但其他浏览器则不然不像我想象的那样。

要喊出新消息,有一个拥有输入文本字段的表单。当按下回车键时,表单正在被提交,所以我省略了提交按钮,因为按下回车键就足够了。

<form method="POST" action="" onsubmit="javascript: return shout();" enctype="multipart/form-data">
<input type="text" style="width: 100%;" name="txtShout" id="txtShout" maxlength="600" autocomplete="off" title="Shout!" placeholder="Shout!">
</form>

呼喊函数如下所示:

function shout() {
alert("test");
        // Post shout and clear textField
        if(getLength("txtShout")) {
            AjaxUpdate("./includes/shout.php?message=" + getItemValue("txtShout"), refreshShoutBox);
            setItemValue("txtShout", "");
        }

        // Stop submit
        return false;
    }

通常,脚本应该调用呼喊函数,AJAX 将发送请求以添加呼喊,然后返回 false,因此不会提交表单。

但在除 Google Chrome 之外的所有浏览器中,表单都会被提交。我在函数中放入了一个alert()来检查它是否被调用或编码错误,但警报没有显示。

I am designin a shoutbox which is AJAX based.
http://businessgame.be/shoutbox.php

The script works perfectly in google chrome, but other browsers don't act like I expect.

To shout a new message, there is a form which owns a input text field. When pressing enter, the form is being submitted, so I have omitted a submit button since pressing enter is sufficient.

<form method="POST" action="" onsubmit="javascript: return shout();" enctype="multipart/form-data">
<input type="text" style="width: 100%;" name="txtShout" id="txtShout" maxlength="600" autocomplete="off" title="Shout!" placeholder="Shout!">
</form>

The shout function looks like this:

function shout() {
alert("test");
        // Post shout and clear textField
        if(getLength("txtShout")) {
            AjaxUpdate("./includes/shout.php?message=" + getItemValue("txtShout"), refreshShoutBox);
            setItemValue("txtShout", "");
        }

        // Stop submit
        return false;
    }

Normally, the script should call the shout function, the AJAX would send a request to add the shout and then return false so the form does not get submitted.

But in all browsers except Google Chrome, the form gets submitted anyway. I put in an alert() in the function to check if it was called or a coding mistake but the alert is not being shown.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2024-12-15 20:09:14

好吧,由于某种原因,我无法在其他浏览器中使用 onsubmit 函数。决定采取不同的方法,而不是拼命地继续寻找解决方案。现在,我刚刚向文本框添加了一个侦听器,以检查何时按下某个键(如果是回车键),然后调用该函数。

这似乎有效,但仍然必须删除表单,因为否则它会被提交^^。

所以现在我得到了这样的结果:

function shoutEnterListener() {
    // Get object
    var domObject = document.getElementById("txtShout");

    // Get shoutbox html code
    if(domObject) {
        domObject.onkeydown = shoutEnter;
        domObject.onkeypress = shoutEnter;
    }
}

function shoutEnter(e) {
    var keyCode;

    // Check which key has been pressed
    if (window.event) {
        keyCode = window.event.keyCode;
    } else {
        keyCode = e.which;
    }

    // If enter, shout!
    if (keyCode == 13) {
        shout();
    }
}

在 init 函数中调用了shoutEnterListener()。这也证明没有任何编码错误,而纯粹是函数根本没有被调用。

如果您仍然找到上一个问题的解决方案,请告诉我,因为这是有点乏味且密集的代码。

Well, for some reason, I couldn't get the onsubmit function working in other browsers. Instead of desperately keep looking to fix it a decided to go for a different approach. Now I just added a listener to the text box to check when a key is pressed if it was the enter key and then call that function.

That seemed to work, still had to remove the form though, because otherwise it would get submitted ^^.

So now I got something like this:

function shoutEnterListener() {
    // Get object
    var domObject = document.getElementById("txtShout");

    // Get shoutbox html code
    if(domObject) {
        domObject.onkeydown = shoutEnter;
        domObject.onkeypress = shoutEnter;
    }
}

function shoutEnter(e) {
    var keyCode;

    // Check which key has been pressed
    if (window.event) {
        keyCode = window.event.keyCode;
    } else {
        keyCode = e.which;
    }

    // If enter, shout!
    if (keyCode == 13) {
        shout();
    }
}

The shoutEnterListener() is called in the init function. This also proves that there wasn't a coding error whatsoever but purely the function not being called at all.

If you still find the solution to the previous problem, let me know, because this is a bit tedious and intensive code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文