文档元素表单操作在javascript中不起作用

时间:2017-11-20 12:05:47

标签: javascript

我正在尝试在javascript中添加表单操作,如下所示:

var form_paypal = document.getElementById("pp1");
function ajax_paypal(orders){
    var htpr = new XMLHttpRequest();
    var url = "http://......";
    var val = "orders=" + orders;
    htpr.open("POST", url, true);
    htpr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    htpr.onreadystatechange = function () {
        if (htpr.readyState == 4 && htpr.status == 200) {
            var sold_out_ids = htpr.responseText;
            if (sold_out_ids > 0) {
                alert("One of your items has sold out! Sorry for any inconvenience");
                //location.reload();
            }else{
                alert("test");
                form_paypal.action = "https://www.sandbox.paypal.com/cgi-bin/webscr";
                //This action is not working
            }

        }
    };
    htpr.send(val);
}

function is_available() {
    var orders = [];
    var x = document.cookie.split(';'); // your array of cookies
    var i = 0;
    x.forEach(item => {
        //to make sure that item contains "order"
        if (item.indexOf('order') > -1) {
            var val = item.split("=");
            orders[i] = val[1]+"o";
            i++;

        }
    });
   ajax_paypal(orders);
}

<form  onclick="is_available()"  method="post" id="pp1">

如果我将它添加到表单标签,它会将用户引导到paypal,但如果我将该操作添加到此功能,它将无法工作。如何正确地向元素添加动作?

1 个答案:

答案 0 :(得分:0)

也许你必须在javascript中添加命令

form_paypal.submit() 
设置动作后