当按下 Enter 时 onsubmit 不会调用我的函数

发布于 2024-10-24 22:22:09 字数 850 浏览 5 评论 0原文

<form onsubmit="submit(); return false;" name="text" id="form" method="get">
<input type="feild" style="font-size: 150%;" id="field" name="field" value=""
onSubmit="this.value='';"/>
<input type="button" id="add" style="font-size: 150%;" value=" Add tally to score "/>
<input type="submit" value="submit" style="visibility:hidden" />

如果我改变“submit();”到“警报('此处');”它完美地调用了警报功能。 为什么它不调用提交函数?

我希望当用户按 Enter 时调用该函数。 http://jsfiddle.net/kaninepete/JsAr4/

注意:侧面的按钮用于不同的目的,而不是问题的一部分。 编辑 更多的工作,尝试遵循基督徒的榜样让我来到这里 http://jsfiddle.net/kaninepete/MGxNG/

我不明白为什么这与他的例子不同! 我得到的只是 "{"error": "请使用 POST 请求"}" 和他的作品都很完美。

<form onsubmit="submit(); return false;" name="text" id="form" method="get">
<input type="feild" style="font-size: 150%;" id="field" name="field" value=""
onSubmit="this.value='';"/>
<input type="button" id="add" style="font-size: 150%;" value=" Add tally to score "/>
<input type="submit" value="submit" style="visibility:hidden" />

If I change "submit();" to "alert('here');" it calls the alert function perfectly.
Why does it not call the submit function?

I want the function to be called when the user presses enter.
http://jsfiddle.net/kaninepete/JsAr4/

note: The button on the side is for a different purpose, and not part of the problem.
edit
More work, trying to follow Christians example has gotten me here http://jsfiddle.net/kaninepete/MGxNG/

I don't see why this is different than his example!
All I get is "{"error": "Please use POST request"}"
and his works perfectly.

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

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

发布评论

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

评论(2

小ぇ时光︴ 2024-10-31 22:22:09

输入元素上没有 onsubmit 方法。

在小提琴上,如果您将输入按钮上的 type="button" 更改为 type="submit" ,那么当您按下按钮时就会调用该表单。

编辑:

你的问题似乎有两件事。

一是 submit 是浏览器中的本机方法。如果您将名称更改为例如。 mySubmit 解决了这个问题。

第二个是 JSFiddle 将 js 代码放在一个范围内。这意味着您定义的方法在该范围之外将不可用。通过使方法全局化可以解决这个问题:

window.mySubmit = function () {
    alert('test');
}

我的小提琴: http://jsfiddle.net/KrooniX/UttWF/

There is no method onsubmit on input elements.

On fiddle, if you change the type="button" to type="submit" on the input button, then the form is called when you press the button.

Edit:

Your problem seems to be two things.

One is that the submit is a native method in the browser. If you change the name to eg. mySubmit solves the problem.

The second is that JSFiddle put the js code inside a scope. This means that the methods you define will not be available outside that scope. This is resoved by making the method global:

window.mySubmit = function () {
    alert('test');
}

My fiddle: http://jsfiddle.net/KrooniX/UttWF/

冷弦 2024-10-31 22:22:09

尝试在 jsfiddle 之外重现,感觉它正在按原样工作。也许我没有得到完整的代码?

这就是我使用的(甚至删除了前面讨论的按钮)。当在文本框中按 Enter 时,此功能有效(在 FF 中测试)。我有什么遗漏的吗?

<html>

<head>

<style>
</style>

<script lang="javascript">

var tally = "0";
window.onload = setup;

function setup() {
    drawTally();
    document.getElementById("add").onclick = addScore;
    document.getElementById("form").onSubmit= submit;
}

function addScore() {
    document.getElementById('change').innerHTML = parseInt(document.getElementById('change').innerHTML)+tally;
    tally=0;
}

function submit(){
    alert("it worked!");
}

function drawTally() {
    var elem = document.getElementById('tallyDisp');
    //var tally = 0;
    if (elem && elem.getContext) {
        var context = elem.getContext('2d');
        if (context) {
            context.fillStyle = '#9f9';
            context.fillRect(0, 0, 300, 200);
            context.fillStyle = '#00f';
            context.font = 'bold 100px sans-serif';
            context.textAlign = 'center';
            context.textBaseline = 'middle';
            context.fillText(tally, 150, 100);
        }
    }
}
setInterval("drawTally();", 60);
</script>


<body>
  <h1>Farkle</h1>
<canvas height="200" width="300" id="tallyDisp">Oops!</canvas>

<form onsubmit="tally=parseInt(tally)+parseInt((text.elements.field.value));
document.text.elements.field.value=''; return false;" id="form" name="text">
    <input type="feild" onsubmit="this.value='';" value="" name="field" id="field" style="font-size: 150%;">

</form>
<p>Your score is <span id="change"> 0</span>!</p>


</body>
</html>

Tried to reproduce outside of jsfiddle, and it feels like it's working as is. Maybe I didn't get the complete code?

This is what I used (even removed the button discussed earlier). This works when pressing Enter in the textbox (tested in FF). Is there something I'm missing?

<html>

<head>

<style>
</style>

<script lang="javascript">

var tally = "0";
window.onload = setup;

function setup() {
    drawTally();
    document.getElementById("add").onclick = addScore;
    document.getElementById("form").onSubmit= submit;
}

function addScore() {
    document.getElementById('change').innerHTML = parseInt(document.getElementById('change').innerHTML)+tally;
    tally=0;
}

function submit(){
    alert("it worked!");
}

function drawTally() {
    var elem = document.getElementById('tallyDisp');
    //var tally = 0;
    if (elem && elem.getContext) {
        var context = elem.getContext('2d');
        if (context) {
            context.fillStyle = '#9f9';
            context.fillRect(0, 0, 300, 200);
            context.fillStyle = '#00f';
            context.font = 'bold 100px sans-serif';
            context.textAlign = 'center';
            context.textBaseline = 'middle';
            context.fillText(tally, 150, 100);
        }
    }
}
setInterval("drawTally();", 60);
</script>


<body>
  <h1>Farkle</h1>
<canvas height="200" width="300" id="tallyDisp">Oops!</canvas>

<form onsubmit="tally=parseInt(tally)+parseInt((text.elements.field.value));
document.text.elements.field.value=''; return false;" id="form" name="text">
    <input type="feild" onsubmit="this.value='';" value="" name="field" id="field" style="font-size: 150%;">

</form>
<p>Your score is <span id="change"> 0</span>!</p>


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