简体   繁体   English

JavaScript函数仅发送一个post参数

[英]JavaScript function sending only one post parameter

I have a JavaScript function to send two POST parameters, one variable and one hardcoded. 我有一个JavaScript函数可以发送两个POST参数,一个变量和一个硬编码。 However, when this function sends POST parameters, actually it sends only the first parameter and not the second. 但是,此函数发送POST参数时,实际上仅发送第一个参数,而不发送第二个参数。 I can't figure out why is this happening The function code is : 我不知道为什么会发生此功能代码是:

function post_to_url(path, params, method) {

    method = method || "post"; // Set method to post by default if not specified.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "sortvalue");
    hiddenField.setAttribute("value", params);

    var hiddenFieldMode = document.createElement("input2");
    hiddenFieldMode.setAttribute("type", "hidden");
    hiddenFieldMode.setAttribute("name", "mode");
    hiddenFieldMode.setAttribute("value", "showmain");
    form.appendChild(hiddenField);
    form.appendChild(hiddenFieldMode);
    document.body.appendChild(form);
    form.submit();

}

I saw the POST data and it is always sortvalue=givenvalue and not sortvalue=givenvalue&mode=showmain as expected.. 我看到了POST数据,它始终是sortvalue=givenvalue sortvalue=givenvalue&mode=showmain而不是按预期的sortvalue=givenvalue&mode=showmain

Because you created an <input2> element. 因为您创建了<input2>元素。

var hiddenFieldMode = document.createElement("input2");

Change it to: 更改为:

var hiddenFieldMode = document.createElement("input");

You're creating an element with the tag name "input2". 您正在使用标签名称“ input2”创建一个元素。 That's not going to be interpreted as an <input> element. 那不会被解释为<input>元素。

There's no need to use .setAttribute() (in any browser) to set the properties of your new DOM nodes: 无需使用.setAttribute() (在任何浏览器中)来设置新DOM节点的属性:

var hiddenFieldMode = document.createElement("input");
hiddenFieldMode.type = "hidden";
hiddenFieldMode.name = "mode";
hiddenFieldMode.value = "showmain";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM