根据URL参数选择下拉列表-PHP还是jQuery? [英] Dropdown selected based on URL parameter - PHP or jQuery?

查看:80
本文介绍了根据URL参数选择下拉列表-PHP还是jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为基于URL参数的表单选择输出选定"的最佳方法是什么?

What's the best method to output "selected" for my form select based on the URL parameter?

在我的URL中,我可能有此参数& term = retail.

In my URL I might have this parameter &term=retail.

如何告诉下面的代码选择零售选项?

How could I tell the below code to select the retail option?

<select class="form-control" name="term" id="saleTerm">
        <option value="all-residential" selected>All Residential</option>
        <option value="apartment">&nbsp;&nbsp;&nbsp;Apartment</option>
        <option value="villa">&nbsp;&nbsp;&nbsp;Villa</option>
        <option value="all-commercial">All Commercial</option>
        <option value="office">&nbsp;&nbsp;&nbsp;Office</option>
        <option value="retail">&nbsp;&nbsp;&nbsp;Retail</option>
</select>

推荐答案

使用Javascript的最简单方法:

Easiest approach using Javascript:

var val = location.href.match(/[?&]term=(.*?)[$&]/)[1];   // get params from URL
$('#saleTerm').val(val);   //  assign URL param to select field

PHP方式指Danijel的回答.

PHP way refer to answer by Danijel.

这篇关于根据URL参数选择下拉列表-PHP还是jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆