首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过jQuery动态更新隐藏表单字段

如何通过jQuery动态更新隐藏表单字段
EN

Stack Overflow用户
提问于 2012-08-11 22:51:09
回答 1查看 986关注 0票数 1

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

代码语言:javascript
复制
<input name="asw_options[asw_icon_order]" type="hidden" value="<?php echo $aswicons ?>" />

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

代码语言:javascript
复制
/* 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);
    }
}); 

存储的数据保存如下:

美味,推特,Facebook,Googleplus,Stumbleupon,Pinterest,LinkedIn,Youtube

一切都很顺利。重新排序字段,然后通过jQuery自动保存。

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

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

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

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-11 22:55:02

您在选择器中缺少了一些引号,请尝试如下:

代码语言:javascript
复制
$('input[name="asw_options[asw_icon_order]"]').val(itemList.sortable('toArray').toString());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11918293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档