2012-08-11 83 views
1

我有一個隱藏的表單域,它存儲了jQuery UI排序列表的值。如何通過jQuery動態更新隱藏的表單域

<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); 
    } 
}); 

存儲的數據會保存這樣的:

美味,推特,臉譜,GOOGLEPLUS,StumbleUpon公司,Pinterest的,LinkedIn,YouTube的

一切工作正常。您重新排序字段並保存通過jQuery自動發生。

我的問題是,上面存儲的隱藏字段,我需要能夠通過jQuery動態更新jQuery成功完成訂單的新保存之後。

這是一個WordPress插件,所以我需要更新隱藏的輸入字段,因爲如果用戶點擊「保存」按鈕,它會保存較舊的值,因爲這是隱藏字段的初始「值」。

正如你可以在jQuery代碼中看到的,我認爲我可以將代碼添加到「成功」函數,然後更新隱藏字段,然而它不起作用。

任何幫助將不勝感激。

回答

1

你缺少你選擇一些報價,試試這個:

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

謝謝你的提示答案。完美地工作。不知道我是否在jQuery函數的右側添加了調用。 – Jason 2012-08-11 23:00:48

+0

其中的一個jquery選擇器問題,總是把我趕出來:) – 2012-08-11 23:26:12

+0

很明顯,我也是。感謝幫助。 – Jason 2012-08-11 23:40:36