以模态发送表单数据而不重定向

时间:2015-12-19 17:41:02

标签: javascript php jquery

我有一个模态的表格。它包括一个文件和一些数据。当我点击提交时,它会重定向到表单的目标页面。我希望将数据发送到目标页面而不重定向到目标页面,并且模式应该关闭。

我试过这个但是没有工作:



    $("form#data").submit(function(e) {
         e.preventDefault();
         var formData = new FormData($(this)[0]);

    $.post($(this).attr("doupload.php"), formData, function(data) {
        alert(data);
    });

    return false;
    });

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">File Details</h3>
    </div>
    <div class="modal-body">
        <p><form id='data' action='doupload.php' method='POST' enctype="multipart/form-data">

             <table>
            <tr>
                <td>Title</td>
                <td width="80%"><input type="text" name='modalTitle' id="modalTitle" maxlength="50" required='required'></td>
                <td><div  id="modalTitlError" ></div></td>
            </tr>
            <tr>
                <td>Subject</td>
                <td><input type="text" name='modalSubject' id="modalSubject" maxlength="30" required='required'></td>
                <td id="modalSubjectError"></td>
            </tr>
            <tr>
                <td>Topics</td>
                <td><textarea rows="3" cols="100" name='modalTopics' id="modalTopics" style="width: 100%" maxlength="200" required='required'> </textarea></td>
                <td id="modalTopicsError"></td>
            </tr>
            <tr><td>
            <input type="file" name="file" id="file" required='required'>
            <button>Submit</button>
            </td></tr>

    </table>
    </form>
        </p>
    </div>
    </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

发布请求的url名称不正确,请使用:

$.post($(this).attr("action"), formData, function(data) {
//OR
$.post('doupload.php', formData, function(data) {

而不是:

$.post($(this).attr("doupload.php"), formData, function(data) {

希望这有帮助。