ajax,異步提交form表單,從後臺返回值到前端

<script LANGUAGE="JavaScript">
        $(function() {
            //$("#bbutton").onclick(function(){
                file_change();
            //});
        });
        function file_change() {
            $("#file").change(function () {
                //$Form
                var form = document.createElement('form');
                $("#file").attr('name', $("#file")[0].files[0].name);
                $(form).append($("#file")[0]);
                $('#object_guid').before('<input type="file" name="file" id="file" />');
                file_change();
                $(form).attr("enctype", "multipart/form-data");
                $(form).ajaxSubmit({
                    type: "post",
                    dataType: "json",
                    url: "${pageContext.request.contextPath }/file/add/upload1",
                    async: false,
                    success: function (data) {
                        var res=data;
                        alert(res);
                        alert("成功"+res.object_guid);
                        if (res.status == 1){
                            $('#object_guid').val(res.object_guid);
                            alert("成功"+res.path);
                        } else {
                            console.log(data);
                            alert(data.message);
                        }
                    },
                    error : function(data) {
                        console.log(data);
                        var res = data.responseText;
                        $('#object_guid').val(res);
                        alert(data.status+" 上傳異常 "+res);
                        //alert(data);
                    }
                });
            })
        }
</script>
<form id="myform" action="${pageContext.request.contextPath }/law/add_law"  method="post" >

    title:<input type="text" name="title" id="title" />
    summary:<input type="text" name="summary" id="summary" />
    produce:<input type="text" name="produce" id="produce">
        announce<input type="text" name="announce"  id="announce">
        exec_star<input type="text" name="execStart" id="exec_start" />
        exec_end<input type="text" name="execEnd" id="exec_end" />
    exec_forever<input type="text" name="execForever" id="exec_forever" />
    ann_agency<input type="text" name="annAgency" id="ann_agency" />
        effect_area<input type="text" name="effectArea" id="effect_area" />
        effect_department<input type="text" name="effectDepartment" id="effect_department" />
        text<input type="text" name="text" id="text" />
        上傳附件:<input type="file" name="file" id="file" size="45" value=""/>
        <input type="hidden" name="objectGuid"  id="object_guid" /><br>
        <%--<input type="button" name="button" id="bbutton" value="上傳"/>--%>
    <button type="submit"  class="btn btn-primary" >提交</button>
</form>

相關文章
相關標籤/搜索