我需要将$option选项传递给php脚本。$option位于名为“Options”的隐藏字段中。
基本上,用户有一些可以通过使用诸如拖放之类的东西重新排列的项目。项目存储在数组中,一旦重新安排了项目,数组就会被重新排序和保存。
我试过将getoption($Options);改为get_Options(‘myOptions’),但是问题并没能解决,我必须从隐藏字段中获得$Options。
我花了大量时间在网上搜索例子,但是相关的解决方案和教程并没有起作用,也或许是我智力有限......
array_walk($_POST['order'], 'strip_text_menu_editor');
$order_array = $_POST['order'];
$current_menu = get_option($option);
$new_order = array();
foreach($order_array as $order) {
foreach($current_menu as $key => $value) {
if($order == $key) {
$new_order[] = $value;
}
}
}
update_option($option, $new_order);
echo '<script type="text/javascript" >
jQuery(document).ready(function($) {
var i = 0';
echo "jQuery('#sortable li').each(function(){
var name = 'listItem_' + i;
i++;
});
});
</script>";
die(true);
jQuery:
jQuery(document).ready(function(jQuery) {
jQuery('#sortable li:odd').addClass('stripe');
jQuery('#sortable').sortable({
handle : '.handle',
update: function(event, ui) {
jQuery('#sortable li').removeClass('stripe');
jQuery('#sortable li:odd').addClass('stripe');
var order = jQuery('#sortable').sortable('toArray');
var data = {
action: 'menu_editor_special_action',
order: order,
};
jQuery.post(ajaxurl, data, function(response){
jQuery('#response').html('Got this from the server: ' + response);
});
}
});
});
HTML:
<ul>
<li id="listItem_0" class="">Item A</li>
<li id="listItem_1" class="stripe">Item B</li>
<li id="listItem_2" class="">Item C</li>
<li id="listItem_3" class="stripe">Item D</li>
<li id="listItem_4" class="">Item E</li>
<li id="listItem_5" class="stripe">Item F</li>
</ul>
调用PHP脚本:
function strip_text_menu_editor(&$value, $key) {
$value = str_replace('listItem_', '', $value);
}
From what I can see of the jQuery fragment you've posted, you would need to add the 'option' value to the data array in your post.
var data = {
action: 'menu_editor_special_action',
order: order,
option: $('input[name=option]').val(), //This part is new
};
jQuery.post(ajaxurl, data, function(response){
jQuery('#response').html('Got this from the server: ' + response);
});
Of course I don't know what sort of data you have stored in 'option' and I don't know what your get_option() etc. functions are doing with it to know how you need to send it for your server-side code to understand, but this should get you started.