提交表单不起作用

时间:2013-08-06 11:13:37

标签: ajax onsubmit

这是我的表单HTML&我想在提交上单击的JavaScript代码然后运行函数ExportPublication()

<form  onsubmit="ExportPublication()" class="form-horizontal" id="frmExportPublication" name="frmExportPublication" >
    <div class="control-group">
        <label class="control-label" for="inputPassword">email Pa</label>
        <div class="controls">
            <textarea name="emailBody" class="input-xlarge">file</textarea>
        </div>
    </div>
    <input type="submit" class="btn btn-primary" id="ExportPublicationButton" projectid="" value="submit">
</form>
<script>
function ExportPublication() {
    $.ajax({
        url: '@Url.Action("CreateOutput", "Publication", new { area = "Cartable" })',
        data: $("#frmExportPublication").serialize(),
        type: 'POST',
        beforeSend: function () {
            ajaxWaiting.showWaiting();
        },
        success: function () {
            $("#message").show();
            ajaxWaiting.hideWaiting();
            $('#ExportPublication').modal('hide');
        },
        error: function (req, status, error) {
            ajaxWaiting.hideWaiting();
        }
    });
}
</script>

当我点击提交时,功能无效。我怎么能纠正这个?

2 个答案:

答案 0 :(得分:1)

你的功能应该是这样的:

   function ExportPublication() {
    event.preventDefault();
    $.ajax({
        url: '@Url.Action("CreateOutput", "Publication", new { area = "Cartable" })',
        data: $("#frmExportPublication").serialize(),
        type: 'POST',
        beforeSend: function () {
            ajaxWaiting.showWaiting();
        },
        success: function () {
            $("#message").show();
            ajaxWaiting.hideWaiting();
            $('#ExportPublication').modal('hide');
        },
        error: function (req, status, error) {
            ajaxWaiting.hideWaiting();
        }
    });
    return false;
 }

另外在表单onsubmit函数中应该返回一个值.. ie;

  <form  onsubmit="return ExportPublication()" class="form-horizontal" id="frmExportPublication" name="frmExportPublication" >

答案 1 :(得分:0)

而不是

  <form  onsubmit="ExportPublication()" class="form-horizontal" id="frmExportPublication" name="frmExportPublication" >

如果您可以通过下面的javascript尝试绑定侦听器,就像魅力一样!

document.forms.frmExportPublication.onsubmit = ExportPublication;

fiddle,为了简洁,已从小提琴中移除了你的ajax调用。