从下拉列表中选择选项后传递参数值并刷新页面
乡亲们,
你们好!
当访问者从下拉菜单中选择任何选项时,我想使用 $_GET['q'] 捕获该值,并且它也应该重新加载页面,以便我可以获取 Q 变量的值。
现在发生的事情是,当我选择任何值时,它只是不刷新,而且我也无法传递 Q 值,看起来像是它唯一的客户端,甚至无法执行任何 PHP 数据抓取功能。
这是我正在使用的代码:
<select id="items" onselect="javascript:reloadPage(this)">
<?php foreach($searchWords as $lolachild): ?>
<option value="<?php
echo $host.str_replace(' ','-',$lolachild).'.html'; ?>"><?php echo ucwords($lolachild); ?> </option>
<?php endforeach; ?>
</select>
<script type="text/javascript"><!--
function reloadPage(id) {
document.location.href = location.href + '?id=' + id.value;
}
--></script>
有人可以帮我解决这个问题吗?
干杯 娜塔莎托马斯
Folks,
Greetings!
When the visitor select any option from the dropdown Menu, i want to capture that value with $_GET['q'], and it should reload the page also so that i can grab the valur of Q variable.
Right now what is happening is, when i select any Value, it just does not refresh and i am not able to passon the Q value as well, looks like its only Client side and can not even execure any PHP Data grabbing function.
here is the code i am using:
<select id="items" onselect="javascript:reloadPage(this)">
<?php foreach($searchWords as $lolachild): ?>
<option value="<?php
echo $host.str_replace(' ','-',$lolachild).'.html'; ?>"><?php echo ucwords($lolachild); ?> </option>
<?php endforeach; ?>
</select>
<script type="text/javascript"><!--
function reloadPage(id) {
document.location.href = location.href + '?id=' + id.value;
}
--></script>
Can someone please help me with this, please?
Cheers
Natasha Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要 onchange 事件,而不是 onselect。
不过,您的脚本还会存在其他问题。
无论是否已经存在查询字符串,reloadPage 函数都会添加到 location.href 中。我建议重建它,请参阅 window.location。
您将需要重新选择他们更改为的值。如果将选择框更改为第二个选项,您的页面将重新加载,并且将再次选择第一个选项。
You want the onchange event, not onselect.
There will be other issues though with your script though.
The reloadPage function is adding to the location.href irregardless of if there is already a query string. I'd suggest reconstructing it, see window.location.
You will need to reselect the value they changed it to. If the change the select box to the second option, your page will reload and the first option will be selected again.