Как передать переменную внутри функции jquery $ .each ($ ("abc") ...? - PullRequest
0 голосов
/ 30 марта 2012

Я пытаюсь перебрать кучу раскрывающихся полей SELECT OPTION html, а из тех, которые НЕ являются пустыми, взять значения и добавить скрытое поле для корзины покупок PAYPAL.

Моя проблемав том, что по какой-то причине переменная curitem не передается в функцию each, и я не могу добавить скрытое поле, как они должны.Все, что я получаю, это "NaN" или "undefined".

Что ожидает PAYPAL: item_name_1, item_name_2 и т. Д. Все числа должны повторяться на + 1.

Как я могу это сделать?

Большое спасибо заранее

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;
    }
});

1 Ответ

1 голос
/ 30 марта 2012

Я думаю, вам нужно инициализировать переменную curitem.

var curitem = 1;

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...