【问题标题】:How to update hidden form field through jQuery dynamically如何通过jQuery动态更新隐藏的表单域
【发布时间】:2012-08-11 22:51:09
【问题描述】:

我有一个隐藏的表单字段,用于存储 jQuery UI 可排序列表的值。

<input name="asw_options[asw_icon_order]" type="hidden" value="<?php echo $aswicons ?>" />

我有一个 jQuery 函数可以保存排序列表的顺序。

/* Social Networking Icon Sorter */
var itemList = $('#asw-sortable');

itemList.sortable({
    update: function(event, ui) {
        $('#loading-animation').show(); // Show the animate loading gif while waiting

        opts = {
            url: ajaxurl, // ajaxurl is defined by WordPress and points to /wp-admin/admin-ajax.php
            type: 'POST',
            async: true,
            cache: false,
            dataType: 'json',
            data:{
                action: 'item_sort', // Tell WordPress how to handle this ajax request
                order: itemList.sortable('toArray').toString() // Passes ID's of list items in  1,3,2 format

            },
            success: function(response) {
                $('#loading-animation').hide(); // Hide the loading animation
                //$('#asw_options[asw_icon_order]').val(itemList.sortable('toArray').toString()); // Update hidden field with new values
                $('input[name=asw_options[asw_icon_order]]').val(itemList.sortable('toArray').toString());
                return; 
            },
            error: function(xhr,textStatus,e) {  // This can be expanded to provide more information
                alert(e);
                // alert('There was an error saving the updates');
                $('#loading-animation').hide(); // Hide the loading animation
                return; 
            }
        };
        $.ajax(opts);
    }
}); 

存储的数据是这样保存的:

Delicious、Twitter、Facebook、Googleplus、Stumbleupon、Pinterest、LinkedIn、Youtube

一切正常。您对字段重新排序,然后通过 jQuery 自动保存。

我的问题是上面存储的隐藏字段,在jQuery成功完成订单的新保存后,我需要能够通过jQuery动态更新它。

这是一个 WordPress 插件,所以我需要更新隐藏的输入字段,因为如果用户点击“保存”按钮,它会保存旧值,因为这就是隐藏字段的初始“值”。

正如您在 jQuery 代码中看到的那样,我认为我可以将代码添加到“成功”函数中,然后更新隐藏字段,但是它不起作用。

任何帮助将不胜感激。

【问题讨论】:

    标签: jquery dynamic hidden-field


    【解决方案1】:

    您的选择器中缺少一些引号,试试这个:

    $('input[name="asw_options[asw_icon_order]"]').val(itemList.sortable('toArray').toString());
    

    【讨论】:

    • 感谢您的及时答复。完美运行。不确定我是否在 jQuery 函数的右侧部分添加了调用。
    • 它是 jquery 选择器问题之一,总是让我抓狂:)
    • 显然,我也是。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多