提前感谢您提供任何帮助我是javascript的新手,所以如果我的某些术语有误,请耐心等待。我有下面的代码和函数,需要将函数run的结果传递给函数AddToCart的arameter。我可以让两个函数独立工作,id =“resultPrintValue”总是返回正确的数字值。基本上我如何将这个值输入AddToCart参数?我已经看到了类似的问题,并尝试了一些建议,例如将函数放入参数但所有返回非数字(NaN) - 我似乎缺少了一些东西。
<!-- select option - calls function run onchange -->
<select name="Print" id="Print" onchange="run()" >
<option value="0">select print size </option>
<option value="1">Lush Tapestry Print on Paper 50cm x 25cm £125</option>
<option value="2">Another print £25</option>
<option value="3">he original £4000000</option>
<select>
<!-- code below returns the value (0-3) correctly of selected option. Need to pass this result in function AddToCart -->
<p>Your id is: </p><p id="resultPrintValue"> </p>
<!-- Stuck with code to put value in parameter of function AddToCart - If I hard code a number 0-3 then AddToCart works fine -->
functions are as below
function run() {
document.getElementById("resultPrintValue").innerHTML = document.getElementById("Print").value;
return (resultPrintValue)
}
function AddToCart(x)
{
var product_index = cart_products.indexOf(x);
if (product_index > -1)
{
cart_quantities[x]++;
}
else
{
cart_products.push(x);
cart_quantities[x] = 1;
}
ShowPopup("The print was added to your basket!");
UpdateCart();
}
答案 0 :(得分:0)
不需要作为参数传入。只需将变量设置为等于run函数的返回值即可。
function AddToCart()
{
var x = this.run()
...
}
答案 1 :(得分:0)
var cart_quantities = [];
var cart_products = [];
function run() {
var value = parseInt(document.getElementById("Print").value, 10);
document.getElementById("resultPrintValue").innerHTML = value;
AddToCart(value);
}
function AddToCart(x) {
var product_index = cart_products.indexOf(x);
if (product_index === -1) {
cart_products.push(x);
}
ShowPopup("The print was added to your basket!");
UpdateCart();
}