表单上提交导致引用未找到javascript错误

时间:2015-08-22 02:17:26

标签: javascript jquery

我试图通过xmlHttpRequest - Ajax提交一个文件。问题是,当我单击输入按钮时,表单刷新并尝试将其发布到呈现此页面的操作方法。在提交按钮单击我想发布到controller-fileUploadUrl上的一个动作方法,这是一个HTTP Post返回JSON。似乎我的document.getElement表单提交函数根本没有触发,它发布到这显然是不正确的。 ?/错误码/ ID步骤1 =&形式放大器; ENCTYPE =多%2Fform数据

我正在尝试按照此示例post a via ajax

@model Models.ErrorCodesStepsViewModel
@{
    ViewBag.Title = "Error Codes";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="@Url.Content("~/Content/css/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/css/chosen.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/css/jquery.stepy.css")" rel="Stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery.stepy.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/chosen.jquery.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.MultiFile.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.fancytree.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.fancytree.filter.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/themes/skin-xp/ui.fancytree.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    var errorsSystemList = '@Url.Action("FindErrorSubsystem", "ErrorCode")';
    var instrumentSelectionUrl = '@Url.Action("Step2", "ErrorCode")';
    var fileUploadURL = '@Url.Action("Upload", "ErrorCode")';

    $(function () {
        $('form').stepy({
            backLabel: '<<',
            nextLabel: '>>',
            finishButton: false,
            next: function (index) {
                var v = $("#InsIdLst").chosen().val();
                if (v == null && index == 2) {
                    alert("Please select an Instrument");
                    return false;
                }
                else {
                    var overlay = $('<div></div>').prependTo('body').attr('id', 'overlay');
                    $.ajax({
                        type: 'GET',
                        url: instrumentSelectionUrl,
                        cache: false,
                        datatype: "html",
                        data: $("form").serialize(),
                        success: function (result) {
                            $("#errorCodes").html(result);
                            overlay.remove();
                        }
                    });
                }
                if (index == 3) {
                }
            }
        });
    });

}
    

<script type="text/javascript">



    $(document).ready(function () {
        $("#InsIdLst").chosen({ max_selected_options: 1 });

        $("#errorCodes").fancytree({

            icons: false
        });

        var errorTree;
        errorTree = $("#errorCodes").fancytree("getTree");

        var myform = document.getElementById("#form");


        $(myform).onsubmit = function () {
            alert("x");
            var formdata = new FormData(); //FormData object
            var fileInput = document.getElementById('fileInput');
            //Iterating through each files selected in fileInput
            for (i = 0; i < fileInput.files.length; i++) {
                //Appending each file to FormData object
                formdata.append(fileInput.files[i].name, fileInput.files[i]);
            }
            //Creating an XMLHttpRequest and sending
            var xhr = new XMLHttpRequest();
            xhr.open('POST', fileUploadURL);
            xhr.send(formdata);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    alert(xhr.responseText);
                }
            }
            return false;
        }

    });

</script>



  @using (Html.BeginForm(new { id = "form", enctype = "multipart/form-data" }))
{
    <fieldset title="Select">
        <legend>instrument</legend>
        <div class="bodyContent">
            <span class="rightContent">
                @Html.ListBoxFor(model => model.SelectedInstrumentTypeID, Model.InstrumentTypeIds, new { id = "InsIdLst", name = "listbox", @class = "chosen-select", multiple = "multiple", data_placeholder = "Click here to Select An Instrument...", style = "width:90%;", tabindex = "5" })
            </span>
        </div>
    </fieldset>
    <fieldset title="View">
        <legend>Problem codes</legend>
        <div id="errorCodes">
        </div>
    </fieldset>
    <fieldset title="Upload">
        <legend>upload file</legend>
        <div class="bodyContent">
            <input id="fileInput" type="file" />
            <input type="submit" value="Upload file" />
        </div>
    </fieldset>
    <fieldset title="Review">
        <legend>description two</legend>
        <!-- inputs -->
    </fieldset>
    <fieldset title="Save">
        <legend>description two</legend>
        <!-- inputs -->
    </fieldset>
}

1 个答案:

答案 0 :(得分:0)

由于您使用的是jQuery,因此不应绑定到本机onsubmit事件,而应绑定到submit事件:

var $myForm = $("#myForm").on("submit", function(event) {
   return $.ajax({
       // ...
   }).then(function() { return false; });
});

注意代码中的return false;如果您不返回false,表格将正常提交。

或者,您可以使用简写submit jQuery方法。 See the documentation了解更多信息。