【问题标题】:Clear jeasyui form fields after successful submit提交成功后清除 jeasyui 表单字段
【发布时间】:2016-10-14 18:12:31
【问题描述】:

我在 xoops 模块中使用 jeasyui 表单,一旦数据成功提交,我将尝试在其中清除所有表单字段。

我已经咨询过this question,但它并没有解决我的问题。

我的 HTML:

<div class="easyui-panel" title="Capture Reqs" style "width:100%;
max-width:600px; padding:30px 60px;">

    <form action = "captureReqs_Save.php" id ="ff" class = "easyui-form" 
     method ="post" data-options = "novalidate:true">

        <div style="margin-bottom:20px"> Area <br>
        <input id="idArea" class="easyui-combobox" style="width:260px" name="idArea" 
            data-options="
            url:'areasJson.php?idZona=<?php echo $idZone; ?>',
            label:'Area:',
            valueField: 'id',
            textField: 'desc',
            required:true
        ">
        </div>

        <div style="margin-bottom:20px"> Material 
        <input id="IdMaterial" class="easyui-combobox" style="width:100%"
         name="IdMaterial" data-options="
            loader: myloader,
            mode: 'remote',
            valueField: 'code',
            textField: 'desc',
            required:true
        ">

        <div style="margin-bottom:20px"> Qty
            <input class="easyui-textbox" name="quantity" style="width:100%"
             data-options="label:'Qty:',required:true, validType:'number'">
        </div>


        <div style="margin-bottom:20px">            
    </form>

    <div style="text-align:center;padding:5px 0">
        <a href="javascript:void(0)" class="easyui-linkbutton" 
         onClick = "submitForm()" style="width:80px"> Submit</a>

        <a href="javascript:void(0)" class="easyui-linkbutton" 
         onClick = "resetForm()" style = "width:80px"> Clear </a>
    </div>
</div>

脚本:

<script>
    var myloader = function (param, success, error) {
        var q = param.q || '';
        if (q.length <= 2) {
            return false
        }
        $.ajax({
            url: 'materialJson.php?idArea=' + $('#idArea').combobox('getValue'),
            dataType: 'json',
            data: {
                q: q
            },
            success: function (data) {
                var items = $.map(data, function (item, index) {
                    return {
                        code: item.code,
                        desc: item.desc
                    };
                });
                success(items);
            },
            error: function () {
                error.apply(this, arguments);
            }
        });
    }

    function submitForm() {
        $('#ff').form('submit', {
            onSubmit: function () {
                return $(this).form('enableValidation').form('validate');
            }
        });

    }

    function resetForm() {
        $('#ff')[0].reset();
    }
</script>

【问题讨论】:

    标签: javascript html forms jquery-easyui


    【解决方案1】:

    尝试调用resetForm。我转换为使用 promise 样式 ajax 并添加了 resetForm

    var myloader = function (param, success, error) {
        var q = param.q || '';
        if (q.length <= 2) {
            return false
        }
        $.ajax({
            url: 'materialJson.php?idArea=' + $('#idArea').combobox('getValue'),
            dataType: 'json',
            data: {
                q: q
            }
        }).then(function (data) {
            var items = $.map(data, function (item, index) {
                return {
                    code: item.code,
                    desc: item.desc
                };
            });
            success(items);
        }).fail(function () {
            error.apply(this, arguments);
        });
    }
    
    function submitForm() {
        $('#ff').submit(function () {
            if ($(this).form('enableValidation').form('validate')) {
                $.post($(this).attr('action'), $(this).serialize(), function (response) {
                    clearForm();
                });
            }
            return false;
        });
    }
    
    function resetForm() {
        $('#ff')[0].reset();
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多