使用node.js / express / jade。我要上传文件而无需刷新页面,因此请在document.ready()函数中使用.on('submit')函数,如下所示。问题是POST发生了两次,我不知道为什么。帮助会很棒。

HTML:

<form id="upSchema" action="/setVSSUP/6" enctype="multipart/form-data" method="post">
    <table id="tblSFU" class="table table-striped table-hover table-condensed">
        <tbody>
            <tr id="sVSFU">
                <td>
                    <input type="file" name="schemadoc">
                </td>
                <td>
                    <button onclick="onSSFUp(6)">Upload</button>
                </td>
            </tr>
        </tbody>
    </table>
</form>


客户端Javscript:

function onSSFUp(key) {
  $('#upSchema').submit();
}


准备好文档:

$(document).ready(function() {
  $(document).on('submit','#upSchema', function(e) {
    $('#schup').text('Starting Upload...');
    e.preventDefault();
    $(this).ajaxSubmit({
      error: function(xhr) {
        $('#schup').text('Error: '+xhr.status);
      },
      success: function(res) {
        $('#schup').text('File upload Success: '+res.name);
      }
    });
    document.getElementById('btnSSFU').disabled = false;
    return false;
  });
});


结果控制台:

POST /setVSSUP/6 200 81ms
POST /setVSSUP/6 200 114ms

最佳答案

表单内的按钮触发它提交。然后,您要向按钮(onSSFUp(6))添加一个额外的onclick事件。删除onclick事件,当您单击按钮时,您将看到表单仍然提交。

07-26 09:34
查看更多