简体   繁体   English

无法让jQuery表单提交工作

[英]Can't get jQuery form submit to work

For some reason, when the form button is clicked, the jQuery script I wrote isn't running, does anyone know why? 由于某种原因,当单击表单按钮时,我编写的jQuery脚本未运行,有人知道为什么吗?

<body>
    <form id="inputform" action="google.com">
        <text id="text">Enter Your Number:</text>
        <input id="input" name="input" type="text">
    <input id="submitArea" type="submit" value="">
    </form>
</body>
$('#inputform').submit(function() {
    window.location = "http://mysite.com/";
});

Yes, I imported the jQuery library and everything, I've sourced the external JS file, but I can't figure out why it still isn't working. 是的,我导入了jQuery库以及所有内容,我已经获取了外部JS文件,但是我不知道为什么它仍然无法正常工作。

You need to prevent the default action from occuring . 您需要防止发生默认操作 You can do that by using preventDefault action on the event e . 您可以通过对事件e使用preventDefault操作来做到这一点。 Something like this: 像这样:

$(function(){
    $('#inputform').submit(function(e) {
        e.preventDefault();
        window.location = "http://mysite.com/";
    });
});

Assuming your script is inside document ready, else you need to move the script inside `jQuery(function($){.....}); 假设您的脚本已经在文档中准备好了,否则您需要在jQuery(function($){.....});中移动脚本。

You need to prevent the default action of the submit button 您需要阻止“提交”按钮的默认操作

$('#inputform').submit(function(e) {
    window.location = "http://mysite.com/";
    return false; // or call e.preventDefault();
});

Ideally you should not do a window.location call from inside a submit button. 理想情况下,您不应从“提交”按钮内部进行window.location调用。 The data you entered in the Form's text input field wont be automatically posted to the action page if you do so. 如果这样做,您在表单的文本输入字段中输入的数据将不会自动发布到操作页面。

<body>
<form id="inputform" action="http://mysite.com/">
    <text id="text">Enter Your Number:</text>
    <input id="input" name="input" type="text">
<input id="submitArea" type="submit" value="">
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM