ajax,异步提交form表单,从后台返回值到前端

本文介绍了一个使用JavaScript实现的文件上传功能,包括文件选择、表单提交及后台处理流程。涉及表单元素设置、Ajax提交及JSON响应处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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>

162536_KggW_3677751.png162551_nKqR_3677751.png

转载于:https://my.oschina.net/u/3677751/blog/1574598

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值