gpt4 book ai didi

javascript - 使用 javascript 查询字符串

转载 作者:行者123 更新时间:2023-12-03 10:05:05 24 4
gpt4 key购买 nike

我正在尝试使用查询字符串将名字和姓氏拉入网址中,但是我使用的代码不起作用,它没有给出任何结果。我不确定要处理谁。我尝试了不同的方法,但得到了相同的结果,查询似乎很好,但是表单似乎出现了问题。

$(function () {

//grab the entire query string
var query = document.location.search.replace('?', '');

//extract each field/value pair
query = query.split('&');

//run through each pair
for (var i = 0; i < query.length; i++) {

//split up the field/value pair into an array
var field = query[i].split("=");

//target the field and assign its value
$("input[name='" + field[0] + "'], select[name='" + field[0] + "']").val(field[1]);

}
});
</script>
</head>
<body>
<div class="element" id="element3528871_1345643">
<input type="hidden" name="MainContent$0$0$hfCallBack" id="MainContent_0_0_hfCallBack"> <input type="hidden" name="MainContent$0$0$hfViewState" id="MainContent_0_0_hfViewState">
<div id="MainContent_0_0_pnlViewForm" class="form_container">
<div class="formHeader">
<h2 class="formTitle">Query string form filling demo</h2>
<p class="formDescription"></p>
</div>
<div style="display:none;" class="errorSummary">
<p>Please enter the required information</p>
</div>
<div class="fieldItem type-name medField" id="">
<div class="fieldItemInner">
<div class="fieldTitle">
<span class="title">Name</span>
</div>
<div class="fieldContent">
<p class="fieldRow">
<span class="fieldBlock">
<input name="field_92375_1015030" type="text" size="16" class="textField firstname" value="">
<em class="fieldNote">First</em>
</span>
<span class="fieldBlock">
<input name="field_92375_1015031" type="text" size="16" class="textField lastname" value="">
<em class="fieldNote">Last</em>
</span>
</p>
</div>
<div class="fieldInstructions"></div>
</div>
</div>

<!-- REST OF FORM OMITTED FOR BREVITY -->

</div>
</div>
</body>

最佳答案

search=window.location.search;  
search=search.slice(1);
listOfPairs=search.split("&");
keyVal=new Object();
keyValue=new Array();
for(i=0;i<listOfPairs.length;i++){
temp=listOfPairs[i].split("=")
keyVal.key=temp[0];
keyVal.value=temp[1];
keyValue.push(keyVal);
}

关于javascript - 使用 javascript 查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30394593/

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