如何传递一个jQuery函数内的变量$ .each($(“abc”)...?
我想迭代一堆SELECT OPTION html下拉字段,并从那些不是空的,为PAYPAL购物车添加一个隐藏字段
我的问题是,由于某些原因,变量“curitem”没有在each
函数中传递,我不能像他们那样添加隐藏字段。应该我得到的是 “南” 或 “不确定”
什么PAYPAL预计是:ITEM_NAME_1,item_name_2等所有数字必须由+1迭代
如何通过CA。 ñ我这样做?
多谢提前
var curitem;
$.each($("select"), function(index, item) {
var attname = $(this).attr("name");
var nom = $(this).attr("data-nom");
var prix = $(this).attr("data-val");
var partname = attname.substring(0, 1);
var qte = $(this).val();
// i want all my <select option> items that the NAME start with "q" AND have a value selected
if (partname == "q" && isNaN(qte) == false && qte > 0) {
// item name
var inp2 = document.createElement("input");
inp2.setAttribute("type", "hidden");
inp2.setAttribute("id", "item_name_"+curitem);
inp2.setAttribute("name", "item_name_"+curitem);
inp2.setAttribute("value", nom);
// amount
var inp3 = document.createElement("input");
inp3.setAttribute("type", "hidden");
inp3.setAttribute("id", "amount_"+curitem);
inp3.setAttribute("name", "amount_"+curitem);
inp3.setAttribute("value", prix);
// qty
var inp4 = document.createElement("input");
inp4.setAttribute("type", "hidden");
inp4.setAttribute("id", "quantity_"+curitem);
inp4.setAttribute("name", "quantity_"+curitem);
inp4.setAttribute("value", qte);
// add hidden fields to form
document.getElementById('payPalForm').appendChild(inp2);
document.getElementById('payPalForm').appendChild(inp3);
document.getElementById('payPalForm').appendChild(inp4);
// item number
curitem = curitem + 1;
}
});
我认为你必须初始化curitem变量。
var curitem = 1;
它在第一行代码中被初始化。如果我把它放入jQuery中,它不起作用。 – Joel 2012-03-30 05:49:29
var curitem在第一行中未定义。你需要给它一个值才能使它工作“curitem = curitem + 1;” – 2012-03-30 14:33:58
好的...这很奇怪。起初,“curitem”WAS的定义就像你告诉我的一样。然后,当我在这里发布时,我只是在尝试'var curitem;'。然后,只是玩耍,我把它改成'var i = 0;'而且它工作。我不知道我的Macrabbit Espresso软件是不是在创建我无法'看见'的错误... – Joel 2012-03-30 15:28:44
为什么不设置'VAR curitem = 0;'? – 2012-03-30 05:31:35
还有一件事:你可以通过$ .data()方法访问你的“data-”标签属性 – 2012-03-30 05:33:28
为什么不用'index'来代替?而'$('select')。each(function(index,item){...})'可能会更好。 – 2012-03-30 05:34:40