gpt4 book ai didi

javascript - 尝试从查询字符串中提取信息

转载 作者:行者123 更新时间:2023-11-28 08:24:04 25 4
gpt4 key购买 nike

我是 JavaScript 新手,我正在尝试从我创建的网络上的查询字符串中提取信息,如果我在没有查询字符串的情况下直接加载页面,页面将加载,但一旦我从表单页面重定向到我正在解析的页面卡住并崩溃...任何人都可以帮忙! :(

http://main.xfiddle.com/7d679c3a/Project1/Commission.php
http://main.xfiddle.com/7d679c3a/Project1/contactForm.php
http://main.xfiddle.com/7d679c3a/Project1/DiceRoll.php
http://main.xfiddle.com/7d679c3a/Project1/IsEven.php
http://main.xfiddle.com/7d679c3a/Project1/palindrome.php
http://main.xfiddle.com/7d679c3a/Project1/part1.php
http://main.xfiddle.com/7d679c3a/Project1/passwordStrength.php
http://main.xfiddle.com/7d679c3a/Project1/allinOne.php

JavaScript 代码

var $ = function(id)
{
return document.getElementById(id);
}

var formInfo = location.search();
formInfo = formInfo.substring(1, formInfo.length);

while (formInfo.indexOf("+") != -1)
{
formInfo = formInfo.replace("+", " ");
}



while (formInfo.indexOf("=") != -1)
{
formInfo.replace("=", " ");
}

formInfo = decodeURI(formInfo);
formInfo.replace("firstname", "");
formInfo.replace("lastname", "");
formInfo.replace("phonenumber", "");
formInfo.replace("postalcode", "");
formInfo.replace("startingmoney", "");

var infoArray = formInfo.split("&");

var firstName = infoArray[0];
var lastName = infoArray[1];
var phoneNumber = infoArray[2];
var postalCode = infoArray[3];
var startingMoney = infoArray[4];

$("playername").innerHTML = firstName + " " +lastName;
$("playerinfo").innerHTML = phoneNumber + " " + postalCode;
$("money").innerHTML = " $$" + startingMoney;

HTML 代码

<div id="fireinfo">
<p id="playername"></p><br/>
<p id="playerinfo"></p>
<p id="money"></p>
</div>

我想将从查询字符串中获取的信息输出到玩家姓名、玩家信息和资金 ID 中。

最佳答案

使用此函数获取查询字符串值:

   function getQueryString(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(window.location.search);

return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};

var firstname = getQueryString('firstname');

关于javascript - 尝试从查询字符串中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22634808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com