4

我正在尝试向if我的函数添加一条语句,但它不起作用。(所有变量都已经在我的完整代码中声明了。)

这是原始代码:

function processCheckout() {
    //static paypal request arguments
    var pp_settings = {
        cmd: '_cart',
        upload: 1,
        no_note: 0,
        bn: 'JQPayPalShop_ShoppingCart_EC_US',
        tax: 0,
        rm: 2,
        custom: ''
    };

    //copy settings.paypal to pp_settings 
    $.extend(pp_settings, settings.paypal);

    //create form for POST request
    var form = $('<form />');
    form.attr('action', 'https://www.paypal.com/cgi-bin/webscr');
    form.attr('method', 'post');
    form.attr('target', '_blank');

    //add paypal variables
    var arg;
    for (var key in pp_settings) {
        arg = $('<input type="hidden" />');
        arg.attr('name', key);
        arg.attr('value', pp_settings[key]);
        //add to form
        form.append(arg);
    }

    //now process items in cart
    var item_index = 0;
    //properties map for 'cart' to the paypal variables
    var map = {
        name: 'item_name',
        quantity: 'quantity',
        checkout_price: 'amount',
        shipping: 'shipping',
        number: 'item_number',
        handling: 'handling'
    };

    for (var g in cart) {
        //group
        for (var i in cart[g]) {
            //item
            if (i == 'length')
                continue;
            //skip length property
            item_index++;
            //process item
            for (var k in map) {
                arg = $('<input type="hidden" />');
                arg.attr('name', map[k] + '_' + item_index);
                arg.attr('value', cart[g][i][k]);
                form.append(arg);
            }
        }
    }

    //add form to the document
    shop.append(form);
    form.submit();
    //remove form
    shop.remove(form);
}

这是我试图修改的代码:

function processCheckout() {
    if (canBuy = false)
    {
        alert("False");
    }
    else
    {
        //static paypal request arguments
        var pp_settings = {
            cmd: '_cart',
            upload: 1,
            no_note: 0,
            bn: 'JQPayPalShop_ShoppingCart_EC_US',
            tax: 0,
            rm: 2,
            custom: ''
        };

        //copy settings.paypal to pp_settings 
        $.extend(pp_settings, settings.paypal);

        //create form for POST request
        var form = $('<form />');
        form.attr('action', 'https://www.paypal.com/cgi-bin/webscr');
        form.attr('method', 'post');
        form.attr('target', '_blank');

        //add paypal variables
        var arg;
        for (var key in pp_settings) {
            arg = $('<input type="hidden" />');
            arg.attr('name', key);
            arg.attr('value', pp_settings[key]);
            //add to form
            form.append(arg);
        }

        //now process items in cart
        var item_index = 0;
        //properties map for 'cart' to the paypal variables
        var map = {
            name: 'item_name',
            quantity: 'quantity',
            checkout_price: 'amount',
            shipping: 'shipping',
            number: 'item_number',
            handling: 'handling'
        };

        for (var g in cart) {
            //group
            for (var i in cart[g]) {
                //item
                if (i == 'length')
                    continue;
                //skip length property
                item_index++;
                //process item
                for (var k in map) {
                    arg = $('<input type="hidden" />');
                    arg.attr('name', map[k] + '_' + item_index);
                    arg.attr('value', cart[g][i][k]);
                    form.append(arg);
                }
            }
        }

        //add form to the document
        shop.append(form);
        form.submit();
        //remove form
        shop.remove(form);
    }
}

我希望整个函数只有在canBuy变量等于true, else, 时才能工作alert("False")

4

5 回答 5

8
// WRONG
if (canBuy = false)

// GOOD
if (canBuy == false)

// BETTER
if (!canBuy)
于 2012-06-20T20:52:01.073 回答
6

if语句应该使用==(comparison) 而不是=(assignment)

if (canBuy = false)

改成...

if (canBuy == false)
于 2012-06-20T20:51:12.783 回答
3

更改if (canBuy = false)if (canBuy == false)

你错过了一个额外的等号。

于 2012-06-20T20:50:47.213 回答
2
if (canBuy = false) 

做作业,应该是==为了比较

if (canBuy == false) 
于 2012-06-20T20:51:35.393 回答
2

假设canBuy在某处声明了processCheckout函数可以看到它,请将初始比较更改为==or===或 go with if (!canBuy) {or if (canBuy) {(取决于您喜欢阅读逻辑的方式)。

//Assignment
if (canBuy = false) {...};    //assignment, won't work.

//Coerced comparison
if (canBuy == false) {...}    //this would work

//Non-coerced compparison
if (canBuy === false) {...}   //if canBuy is an actual boolean, this would work

//Also (in the category of "is more readable"...)
if (!canBuy) {      //this would work
    //...
}
if (canBuy) {       //as would this
    //...
}
于 2012-06-20T21:05:11.530 回答