简体   繁体   English

为什么在 url 中的表单中看不到查询参数?

[英]Why can't I see the query parameters in the form present in the url?

I'm having a problem getting the form values by passing the two query parameters, id and type, from the url.我在通过从 url 传递两个查询参数 id 和 type 来获取表单值时遇到问题。

Let's assume the URL is:假设 URL 是:

... page.html?id=14&type=Title ... page.html?id=14&type=Title

I want the values of these to be shown in the form to then make the change later.我希望这些值显示在表单中,以便稍后进行更改。

 function getQueryVariable () { var query = window.location.search.substring (1); var vars = query.split ("&"); for (var i = 0; i <vars.length; i ++) { var pair = vars [i].split ("="); } } function onLoad () { var value = getQueryVariable (); var id = document.getElementById ('id'); var type = document.getElementById ('type'); id.value = value; type.value = value; }
 <div class = "container"> <form method ="post" id="save" action="javascript: myFunction()" onload="onLoad()"> <div class = "field"> <label for = "id"> ID: </label> <input type = "number" id = "id" name = "id" /> </div> <div class = "field"> <label for = "type"> Fragment Type: </label> <input type = "text" id = "type" name = "type" /> </div> <div class = "field"> <button type = "submit" class = "full"> Save changes </button> </div> </form> </div>

As you can see from the code, I call the onLoad() function to load the data into the form.从代码中可以看出,我调用了 onLoad() function 将数据加载到表单中。

I don't get any errors;我没有收到任何错误; the values of the getQueryVariable() function variables are correct, but I notice that it is not called. getQueryVariable() function 变量的值是正确的,但我注意到它没有被调用。

myFunction() is not shown, but this is the function that will serve me later to modify the fields of the form. myFunction() 未显示,但这是 function 稍后将用于修改表单的字段。

Could you kindly help me?你能帮帮我吗?

Your getQueryVariable () function is not returning anything.您的getQueryVariable () function 没有返回任何内容。 You can try something like this:你可以尝试这样的事情:

function getQueryVariable () {
             var query = window.location.search.substring (1);
             var vars = query.split ("&");
             var params = {}
             for (var i = 0; i <vars.length; i ++) {
                 var pair = vars [i] .split ("=");
                 params[pair[0]] = pair[1]
             }
             return params
         }

         function onLoad () {
             var params = getQueryVariable ();
             var id = document.getElementById ('id');
             var type = document.getElementById ('type');
             id.value = params.id ;
             type.value = params.type ;
         }

First of all, you need to execute onLoad function, then you need to create an object to store the values and pass it to another function.首先,您需要执行onLoad function,然后您需要创建一个object 来存储值并将其传递给另一个function。 And your loop is really out of the world, I corrected it, please try to understand my code and if you have questions feel free to comment.而且你的循环真的太棒了,我更正了它,请尝试理解我的代码,如果你有任何问题,请随时发表评论。 Here you go, for query, I changed it to a string that you provided as an exemple, so it would work in snippet.在这里,您是 go,为了查询,我将其更改为您作为示例提供的字符串,因此它可以在片段中使用。

 function getQueryVariable () { var object = {} var query = "id=14&type=Hello%20Title"; var vars = query.split ("&"); for (var i = 0; i <vars.length; i ++) { let splitted = vars[i].split('=') object[splitted[0]] = decodeURI(splitted[1]) } return object; } function onLoad () { var pairs = getQueryVariable (); var id = document.getElementById ('id'); var type = document.getElementById ('type'); id.value = pairs.id; type.value = pairs.type; } onLoad();
 <div class = "container"> <form method ="post" id="save" action="javascript: myFunction()" onload="onLoad()"> <div class = "field"> <label for = "id"> ID: </label> <input type = "number" id = "id" name = "id" /> </div> <div class = "field"> <label for = "type"> Fragment Type: </label> <input type = "text" id = "type" name = "type" /> </div> <div class = "field"> <button type = "submit" class = "full"> Save changes </button> </div> </form> </div>

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

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