简体   繁体   English

通过ajax发送表单,选择html节点进行数据发送

[英]sending form via ajax, html node select for data sending

with this code 用这个代码

<script>
// this is the class of the submit button
$(".update_form").click(function() { // changed
    $.ajax({
           type: "POST",
           url: "approve_test.php",
           data: $(this).parent().serialize(), // changed
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });
    return false; // avoid to execute the actual submit of the form.
});
</script>

from here Targeting multiple forms to send via ajax (jquery) 从这里定位要通过ajax发送的多种表单(jquery)

user can send data of specific form. 用户可以发送特定格式的数据。

In this example structure is like this 在这个例子中,结构是这样的

<form id="form1" method="post">
    <input type="text" id="name1" name="value" value="">
    <input type="submit"  class="update_form"  value="Save Changes"> <!-- changed -->
  </form>

 <form id="form2" method="post">
    <input type="text" id="name2" name="value" value="">
    <input type="submit"  class="update_form"  value="Save Changes"> <!-- changed -->
 </form>

In my case I've a bit more complicated Html structure. 就我而言,我的HTML结构更为复杂。

<form id="w0" action="/users/new_article" method="post" enctype="multipart/form-data">
<input type="hidden" name="_csrf" value="yeUp">    <div class="form-group field-uploadform-file">
<label class="control-label" for="uploadform-file">File</label>
<input type="hidden" name="UploadForm[file]" value=""><input type="file" id="uploadform-file" name="UploadForm[file]">

<div class="help-block"></div>
</div>

 <!--   <div class="file-input btn btn-block btn-primary"> -->
    <div class="file-input btn btn-block btn-primary">
        + add files
        <input type="file" name="files" id="image_upload">
    </div>
    </form>

I'm monitoring change of #image_upload. 我正在监视#image_upload的更改。 But it is not child of form tag, thus I cannot use this code from first example 但这不是表单标签的子级,因此我无法使用第一个示例中的此代码

data: $(this).parent().serialize(), // changed

So my question is how do I must write my code so the form submits ? 所以我的问题是我该如何编写代码以便表单提交?

You can't submit files as using ajax, as easily as you could submit just text. 您不能像使用ajax一样提交文件,就像提交文本一样容易。

You must use XHR. 您必须使用XHR。

Here's a solution: How can I upload files asynchronously? 这是一个解决方案: 如何异步上传文件?

Take note that you're pretty much out of luck if you need to support IE8/9. 请注意,如果您需要支持IE8 / 9,那么您几乎不走运。

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

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