简体   繁体   English

提交具有动作和提交功能的表格

[英]Submit Form With Both Action and Onsubmit Function

My JS is not that great so I have been fiddling with this for a while now. 我的JS不太好,所以我已经摆弄了一段时间。

I have a form which is being POST to another file when the submit button is clicked. 单击提交按钮时,我正在将表单发布到另一个文件。 When it is clicked I also want to show an alert then redirect the user back to a URL. 当我单击它时,我也想显示一个警报,然后将用户重定向回URL。

The redirecting code works just fine on a button where I call the function "onclick" like so: 重定向代码在我将函数称为“ onclick”的按钮上可以正常工作,如下所示:

<button onclick="test()">Return</button>

But I don't want to have an extra button for this...I want the form to POST then show an alert box then go to URL specified but I get not a function error from console, thanks. 但是我不想为此设置一个额外的按钮...我希望表单发布,然后显示一个警告框,然后转到指定的URL,但是我没有从控制台收到功能错误,谢谢。

<iframe name="noreloadhack" style="display:none;"></iframe>
<form action="http://www.example.com/test.php" onsubmit="return test();" method="post" target="noreloadhack">

JS: JS:

 <script>
 function test() {
     alert('Hello World');
     var return_url = document.getElementById('return_url').value;
     window.location.href= return_url;
 }
 </script>

If it makes a difference I have the form target set to a hidden iframe as a hack to not reload page on submit (I know, not the best method). 如果有什么不同,我可以将表单目标设置为隐藏的iframe,作为一种技巧,以便在提交时不重新加载页面(我知道,这不是最好的方法)。 I'm pretty much using 4 form attributes here. 我在这里几乎使用了4种表单属性。

I have some old code that I used to solve a similar situation. 我有一些旧代码用来解决类似情况。 Where I wanted to submit a form but not reload the page, here it is. 我想提交表单但不重新加载页面的位置在这里。 Since there were only 4 input fields I just grabbed the values using jquery. 由于只有4个输入字段,因此我只是使用jquery来获取值。

Javascript: 使用Javascript:

 function processForm() {
        var teamMembers=new Array();
        console.log($("#"));
        var schoolName=$("#schoolname").val();
        var teamMembers=new Array();
        teamMembers.push($("#contestant1").val());
        teamMembers.push($("#contestant2").val());
        teamMembers.push($("#contestant3").val());
        $.ajax({
                method: "POST",
                url: "php/register.php",
                data: { schoolname: schoolName, teammembers:teamMembers.toString()}
            })
            .done(function( msg ) {
                alert( "Your team is now registered " + msg );
                $('#register').hide();
                location.reload();
            });
        // You must return false to prevent the default form behavior
        // default being reloading the page
        return false;
    }

HTML: HTML:

<form id="registration_form" onsubmit="return processForm()" method="POST">
<p style="margin:0px;">School Name:</p>
<input type="text" id="schoolname" name="schoolname" autocomplete="off" class="input" required>
<hr>
<p style="margin:0px;">Contestants</p>
<div id="teammembers">
    <input type="text" id="contestant1" name="contestant1" autocomplete="off" class="input" required>
    <p></p>
    <input type="text" id="contestant2" name="contestant2" autocomplete="off" class="input" required>
    <p></p>
    <input type="text" id="contestant3" name="contestant3" autocomplete="off" class="input" required>
</div>
<input type="submit" id="registered">

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

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