我正在开发一个“添加到购物车”按钮,它将商品的 ID 和数量传递给 Javascript 函数。该函数处理这些值,创建一个临时表单来发布这些值,然后重新加载产品页面,并将 ?AddItem 添加到 url。
这是工作代码:
function submitItem( ID, Code)
{
alert("Submission received"); //--This is being triggered
var toCart, inputA, inputB, quantity;
quantity = Code.value;
// Create form
toCart = document.createElement( 'form' );
toCart.action = " &AddItem";
toCart.method = 'post';
//Rest of the code excutes as normal
}
我的产品页面是动态加载的,因此类别的 url 将是 blah.com/products.php?cat=FOO,但 AddItem 删除了 cat= 子句,因此加载了一个空白页面。为了避免这种情况,我尝试将类别前缀 (FOO) 传递给 javascript,但添加第三个参数会破坏它。
这是我的新代码:
function submitItemCat( ID, Code, Category )
{
alert("Submission received"); //--This is not being triggered
var toCart, inputA, inputB, quantity;
quantity = Code.value;
// Create form
toCart = document.createElement( 'form' );
toCart.action = " &AddItem"+ Category; //--This is were I amend the URL
toCart.method = 'post';
//Rest of the code
}
和html中的函数调用
$jsCat = json_encode($_GET["cat"]); // This is FOO
echo'
<div id= "QuantBox">
<label for="Quantity">'.$catPrefix.'</label>
<input type = "text" name= "Quantity" id= "Q'.$row["StockCode"].'" value="1">
<a
class= "AddToCart"
onclick= "submitItemCat( '.$row["StockID"].', Q'.$row["StockCode"].', '.$jsCat.' )" >
</a>
</div>';
我也尝试将第三个变量传递为 "FOO", $_GET["cat"] 但它们都不起作用。非常感谢任何建议。