これが完全に間違っているかどうかはわかりませんが、私がやろうとしていることが可能であることを願っています.
ユーザーから「選択された」チェックボックスを保存し、それらを $_SESSION['order_items'][index]['cheeseids'][]; に保存する既存の ajax jquery 関数があります。ユーザーがこの「アイテム」をリコールするとき、以前と同じチェックボックスを「選択」したいと思います。以下の例を参照してください。
[order_items] => Array
(
[1] => Array
(
[cheeseids] => Array
(
[0] => 1
[1] => 2
[2] => 4
)
私の「変更/復元」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");
PHP にも追加する JS/jQuery 関数全体:
$("#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>";
}
?>