简体   繁体   English

jQuery获取输入字段的旧值

[英]jQuery gets old value of input field

In the below code, when I change the value in the box and click "Update", jQuery gets the old value of the input field. 在下面的代码中,当我更改框中的值并单击“更新”时,jQuery将获得输入字段的旧值。 When I refresh the page (the new input is retained after the refresh) and click the "Update" again, then the new value is used. 当我刷新页面(刷新后保留新输入)并再次单击“更新”时,将使用新值。 What am I doing wrong? 我究竟做错了什么?

<body>
    <script type="text/javascript">
    $(document).ready(function() {
        var submit_command = function(e) {
            $.getJSON($SCRIPT_ROOT + '/__test',
                e.data,
                function(data) {
                    $("#result").text(data.test);
                    });
            return false;
        };
        $('a#update_parameter').bind('click', {
                                    name: "Param",
                                    test: parseFloat($('input:text[name=input_parameter]').val().replace(",", "."))
                                }, submit_command);
    });
    </script>
<div class="parameter">
    <span class="parameter_name">My Parameter</span>
    <input type="text" name="input_parameter" size=3 />
    <a href=# id="update_parameter">Update</a>
    <span id="result">?</span>
</div>
</body>

Note: Feel free to suggest improvements. 注意:欢迎提出改进建议。

Please try something like this 请尝试这样的事情

$('a').click(function (){
    alert($('input[name=input_parameter]').val());
});

Check the fiddle - http://jsfiddle.net/2bt5pqxb/ - it alerts the inserted value. 检查小提琴-http: //jsfiddle.net/2bt5pqxb/-它会警告插入的值。 I hope this helps 我希望这有帮助

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

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