我不确定我这样做是否完全错了,但我希望我所做的是可能的。
我有一个现有的ajax jquery函数,从用户保存"选定"复选框,并将它们存储在$_SESSION['order_items'][index]['cheeseids'][];当用户回忆起这个"项目"时,我希望有与之前相同的复选框"选中"。请看下面的例子:
[order_items] => Array
(
[1] => Array
(
[cheeseids] => Array
(
[0] => 1
[1] => 2
[2] => 4
)
我的"change/restore" jquery函数使用以下变量获取索引号:
var productIDValSplitter = (this.id).split("_");
var productIDVal = productIDValSplitter[1];
函数我试图使用"重新选择"框1,2,4(从cheeseids数组)不工作(它是在我的jQuery函数)。它实际上会导致整个jQuery代码无法工作:
<?php
foreach($_SESSION['order_items'] as $key => $value)
{
?>
var idnum = <?php echo $value['<script type="text/javascript">productIDVal</script>']['cheeseids'] ?>;
$("div.cheesebox input[type=checkbox]").eq(" + idnum + ").attr("checked", "checked");
<?php
}
?>
JS输出:
var idnum = ;
$("div.cheesebox input[type=checkbox]").eq(" + idnum + ").attr("checked", "checked");
var idnum = ;
$("div.cheesebox input[type=checkbox]").eq(" + idnum + ").attr("checked", "checked");
整个JS/jQuery函数,我也添加了PHP:
$("#basketItemsWrap li img.change").live("click", function(event) {
var productIDValSplitter = (this.id).split("_");
var productIDVal = productIDValSplitter[1];
$("#notificationsLoader").show();
$.ajax({
type: "POST",
url: "includes/ajax/functions.php",
data: { productID: productIDVal, action: "deleteFromBasket"},
success: function(theResponse) {
$("div#order_qty input").attr('value', $("#order_" + productIDVal + " #order_qty").text());
$("div#order_listprice input#price1").attr('value', $("#order_" + productIDVal + " #order_listprice").text().replace("$", ""));
$("div#order_price input#discprice1").attr('value', $("#order_" + productIDVal + " #order_price").text().replace("$", ""));
$("div#order_price input#itemprice1").attr('value', $("#order_" + productIDVal + " #order_itemprice").text().replace("$", ""));
//The following functions restore the order details in the select menus, checkboxes, and radio buttons
//Restores the item selected
for(var i = 0; i < $("#item1 option").size(); i++)
{
if($("#order_" + productIDVal + " #order_item").text() == $("#item1 option:eq("+i+")").text())
{
$("#item1 option:eq("+i+")").attr("selected", "selected");
//$("#item1").val(i);
CalcPrice(1);
}
}
//Restores the promotion selected
for(var i = 0; i < $("#promo1 option").size(); i++)
{
if($("#order_" + productIDVal + " #order_promo").text() == $("#promo1 option:eq("+i+")").text())
{
$("#promo1 option:eq("+i+")").attr("selected", "selected");
//$("#promo1").val(i);
CalcPromo(1);
}
}
<?php foreach($_SESSION['order_items'][1]['cheeseids'] as $id): ?>
$("div.cheesebox input[type=checkbox]#<?php echo $id;?>").attr("checked", "checked");
<?php endforeach; ?>
//Restores the cheese options selected
// $('div.cheesebox input[type=checkbox]').size(); i++)
//$('div.cheesebox input[type=checkbox]').eq(1).attr('checked', 'checked');
$("#order_" + productIDVal).hide("slow", function() {$(this).remove();Calc();});
$("#notificationsLoader").hide();
}
});
});
生成复选框的PHP/HTML代码:
<?php
//Display all "cheese" options in a checkbox group
foreach($_SESSION['ingr_cheese'] as $key => $value)
{
echo "<div class='"cheesebox'" style='"float:left; width:175px; margin-bottom:7px;'">";
echo "<input type='"checkbox'" name='"cheese1'" id='"cheese1'" class='"cheeseCheck'" value='"".$key."'"> <label for='"cheese1'">".$value['cheese']."</label></br>";
echo "</div>";
}
?>
如果您列出的数组已经是$_SESSION
数组中的内容的准确表示,那么这段代码应该执行您正在查找的操作。
<?php foreach($_SESSION['order_items'][1]['cheeseids'] as $id): ?>
$("div.cheesebox input[type=checkbox]#<?php echo $id;?>").attr("checked", "checked");
<?php endfor; ?>
这应该生成如下所示的javascript:
$("div.cheesebox input[type=checkbox]#1").attr("checked", "checked");
$("div.cheesebox input[type=checkbox]#2").attr("checked", "checked");
$("div.cheesebox input[type=checkbox]#4").attr("checked", "checked");
这段代码假设复选框的标记看起来(有点)像这样:
<div class=".cheesebox">
<input type="checkbox" id="1">
<input type="checkbox" id="2">
<input type="checkbox" id="3">
<input type="checkbox" id="4">
</div>
我验证了选择器语法,但如果它不起作用,那么我可能对复选框的标记做了一个错误的假设。如果这还不足以给你指明正确的方向,请粘贴更多的html,我会再试一次。