如何在不重定向的情况下提交多HTML表单

时间:2017-08-19 15:49:12

标签: javascript php jquery html forms

我正在使用此表格

<form action="upload.php" enctype="multipart/form-data" method="post">
        <p class="act-large act-center">Name:&nbsp;<input id="uploaderName" name="uploaderName" required="required" type="text" /></p>

        <p class="act-large act-center">Choose file to send: <input id="fileToUpload" name="fileToUpload" type="file" /> <input name="submit" style="vertical-align:middle" type="submit" value="Send" /></p>
        </form>

并导入Js

<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
    var upload = $("#fileToUpload").val();
    $.post("upload.php", {text: upload});
    $("#fileToUpload").attr("value", "");
    $("#uploaderName").attr("value", "");
    return false;
    });
});
</script>

但是当我提交表单时,它会重定向到upload.php 谁知道这个问题?请帮助我,这是我的任务。

3 个答案:

答案 0 :(得分:0)

你没有在输入中声明id="submit",试试这个:

<form action="upload.php" enctype="multipart/form-data" method="post">
        <p class="act-large act-center">Name:&nbsp;<input id="uploaderName" name="uploaderName" required="required" type="text" /></p>

        <p class="act-large act-center">Choose file to send: <input id="fileToUpload" name="fileToUpload" type="file" /> <input name="submit" style="vertical-align:middle" type="submit" value="Send" id="submit"/></p>
        </form>

答案 1 :(得分:0)

你可以使用$ .ajax方法来实现这个

        jQuery.ajax({
        url: "submit.php",
        data: dataString,
        type: "POST",
        success: function(data){
            $("#myForm").html(data);
        },
        error: function (){}
    });

答案 2 :(得分:0)

试试这个:

<form action="upload.php" enctype="multipart/form-data" method="post">
<p class="act-large act-center">Name:&nbsp;<input id="uploaderName" name="uploaderName" required="required" type="text" /></p>`
<p class="act-large act-center">Choose file to send: <input id="fileToUpload" name="fileToUpload" type="file" /> <input name="submit" style="vertical-align:middle" type="submit" value="Send" id="submit"/></p>
</form>

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

<script type="text/javascript">
 $(document).ready(function(){
  $("#submit").click(function(){    
   var upload = $("#fileToUpload").val();
   $.post("upload.php", {text: upload});
   $("#fileToUpload").attr("value", "");
   $("#uploaderName").attr("value", "");
   return false;
  });
 });
</script>`