简体   繁体   English

如何使用JQuery从自定义输入中获取URL参数

[英]How to get URL parameters from custom input with JQuery

I have a view that I want to store some URL parameters and have tried everything. 我有一个观点,我想存储一些URL参数,并尝试了一切。 Here is my current code. 这是我目前的代码。

Sample URL: https://www.facebook.com?username= ""&password=""(I'm trying to collect the parameter values) 示例网址: https//www.facebook.com?username = “”&password =“”(我正在尝试收集参数值)

HTML: HTML:

<input id="urlInput" type="text" placeholder="Enter URL" class="form-control" />

Javascript: 使用Javascript:

var url = $("#urlInput").val();//This pulls the value of the input/URL with parameters
var getURLUser = url.substring(url.indexOf("?"));
$("#urluser").html(getURLUser);

What is wrong? 怎么了? Thanks. 谢谢。

Currently, you're getting a String from the beginning of ? 目前,你从一开始就得到一个字符串? to the end of the String. 到字符串的末尾。

You could split the string by &password= , and get the right side of it: 您可以通过&password=拆分字符串,并获取它的右侧:

var url = "https://www.facebook.com?username=123&password=(I'm trying to collect the parameter values)";
var getURLUser = url.split("&password=")[1];
// results in "(I'm trying to collect the parameter values)"
console.log(getURLUser);

or 要么

var url = "https://www.facebook.com?username=123&password=(I'm trying to collect the parameter values)";
var getURLUser = url.split("&password=");
console.log(getURLUser[0]);
// results in "https://www.facebook.com?username=123"
console.log(getURLUser[1]);
// results in "(I'm trying to collect the parameter values)"

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

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