简体   繁体   English

Ajax表单选择更改未出现在jQuery .serialize / .val中

[英]Ajax form select changes doesn't appear in jQuery .serialize/.val

I'm retrieving form with .load jQuery function and places it in document body. 我正在使用.load jQuery函数检索表单并将其放在文档主体中。

After that I'm changing select value (manually) and trying to save form with ajax .post request. 之后,我要更改选择值(手动)并尝试使用ajax .post请求保存表单。

Select value retrieved by jQuery in any way (with .val or sereilize) doesn't changes. jQuery以任何方式(使用.val或sereilize)检索的选择值都不会更改。 Value stays as it was rendered. 价值保持不变。

Without prevetnDefault (with plain POST request) form saves as expected. 如果没有prevetnDefault (带有简单的POST请求), 则按预期保存表单。 But both .val on select or .sereilize on form returns old select value (not really selected). 但是select上的.val或form上的.sereilize都返回旧的选择值(未真正选择)。

<form
       id="phrase-fake-form-12243"
       method="POST"
       action="/phrase-fake/change-group/12243/3">

            <input type='hidden' name='csrfmiddlewaretoken' value='UQuHH3ahAnBaSUPsCBaF1QKF4I0O48AO' />
            <select name="group" id="phrase-fake-modal-group-dropdown">

                    <option value="20393"
                            selected="selected">
                        1
                    </option>

                    <option value="20405"
                            >
                        2
                    </option>

                    <option value="20417"
                            >
                        3
                    </option>
            </select>
            <input type="submit" value="Сохранить" class="ui button blue">
        </form>

JS: JS:

<script>

$("#phrase-fake-form-12243").submit(function (event) {

    console.log($('#phrase-fake-form-12243').serialize())
    console.log($('#phrase-fake-modal-group-dropdown').dropdown('get value'))
    $.post('/phrase-fake/change-group/12243/3', $('#phrase-fake-form-12243').serialize())

    event.preventDefault();
});
</script>

What I'm doing wrong? 我做错了什么? Actually it's seems like a bug.. 其实好像是个bug。

Try using this library, form2js 尝试使用此库form2js

You just have to call the method .toObject() on the jQuery object. 您只需要在jQuery对象上调用方法.toObject() For example, if this is your html 例如,如果这是您的html

<form id="test">
...
</form>

You can get the all the form element values by 您可以通过以下方式获取所有表单元素值

var formObject = $("#test").toObject();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM