获取查询字符串值并传递给 JQuery
Get query string values and pass to JQuery
我正在尝试创建自己的自定义面包屑,为此我必须使用 PHP 获取查询字符串,然后将其传递给我的 jQuery。到目前为止,我已经能够在 foreach 循环中获取查询字符串 keys/values
然后将它们放入输入中,然后我可以使用 jQuery 获取值但是,我的问题是如果我有多个像这样的单个键的值;
?filter_style=bevelled-glass&filter_colour=amber,clear
我检索的是两个值作为一个值,但我需要能够将它们分开。这是我的代码和我正在检索的内容;
代码;
<?php
foreach($_GET as $key => $value){
echo '<input class="' . $key . '" type="text" value="' . $value . '" style="display: none;">';
}
?>
输出;
<input class="filter_style" type="text" value="bevelled-glass" style="display: none;">
<input class="filter_colour" type="text" value="amber,clear" style="display: none;">
我想要的;
<input class="filter_style" type="text" value="bevelled-glass" style="display: none;">
<input class="filter_colour" type="text" value="amber" style="display: none;">
<input class="filter_colour" type="text" value="clear" style="display: none;">
此外,这是将此数据传递给我的 jQuery 的最佳方式吗?或者我可以创建某种 oJSONon 对象吗?
您可以展开每个值以从中获取项目数组,然后循环并打印。
<?php
foreach($_GET as $key => $value){
$subarray = explode(",",$value);
foreach($subarray as $subvalue)
{
echo '<input class="' . $key . '" type="text" value="' . $subvalue . '" style="display: none;">';
}
}
?>
试试这样的
foreach($_GET as $key => $value){
if(count(explode(',',$value))){
foreach(explode(',',$value) as $subvalue){
echo '<input class="' . $key . '" type="text" value="' . $subvalue . '" style="display: none;">';
}
}else{
echo '<input class="' . $key . '" type="text" value="' . $value . '" style="display: none;">';
}
}
我对 PHP 了解不多,但根据我对 GET 请求的了解,您应该能够将 amber,clear
作为数组获取并从中分离出来。在伪代码,
foreach ($_GET as $key => $value) {
if ($value is array) {
foreach ($val in $value) {
<input class="' . $key . '" value="' . $val . '">
}
else {
<input class="' . $key . '" value="' . $value . '">
}
}
如果代码 运行 是这样写的,我会感到非常惊讶,但这就是我在用其他语言开发时解决这个问题的方法。另外,在我看来,尽可能使用 JSON 总是更好。
我正在尝试创建自己的自定义面包屑,为此我必须使用 PHP 获取查询字符串,然后将其传递给我的 jQuery。到目前为止,我已经能够在 foreach 循环中获取查询字符串 keys/values
然后将它们放入输入中,然后我可以使用 jQuery 获取值但是,我的问题是如果我有多个像这样的单个键的值;
?filter_style=bevelled-glass&filter_colour=amber,clear
我检索的是两个值作为一个值,但我需要能够将它们分开。这是我的代码和我正在检索的内容;
代码;
<?php
foreach($_GET as $key => $value){
echo '<input class="' . $key . '" type="text" value="' . $value . '" style="display: none;">';
}
?>
输出;
<input class="filter_style" type="text" value="bevelled-glass" style="display: none;">
<input class="filter_colour" type="text" value="amber,clear" style="display: none;">
我想要的;
<input class="filter_style" type="text" value="bevelled-glass" style="display: none;">
<input class="filter_colour" type="text" value="amber" style="display: none;">
<input class="filter_colour" type="text" value="clear" style="display: none;">
此外,这是将此数据传递给我的 jQuery 的最佳方式吗?或者我可以创建某种 oJSONon 对象吗?
您可以展开每个值以从中获取项目数组,然后循环并打印。
<?php
foreach($_GET as $key => $value){
$subarray = explode(",",$value);
foreach($subarray as $subvalue)
{
echo '<input class="' . $key . '" type="text" value="' . $subvalue . '" style="display: none;">';
}
}
?>
试试这样的
foreach($_GET as $key => $value){
if(count(explode(',',$value))){
foreach(explode(',',$value) as $subvalue){
echo '<input class="' . $key . '" type="text" value="' . $subvalue . '" style="display: none;">';
}
}else{
echo '<input class="' . $key . '" type="text" value="' . $value . '" style="display: none;">';
}
}
我对 PHP 了解不多,但根据我对 GET 请求的了解,您应该能够将 amber,clear
作为数组获取并从中分离出来。在伪代码,
foreach ($_GET as $key => $value) {
if ($value is array) {
foreach ($val in $value) {
<input class="' . $key . '" value="' . $val . '">
}
else {
<input class="' . $key . '" value="' . $value . '">
}
}
如果代码 运行 是这样写的,我会感到非常惊讶,但这就是我在用其他语言开发时解决这个问题的方法。另外,在我看来,尽可能使用 JSON 总是更好。