使用Jquery从动态链接下拉列表中取出url信息
美好的一天,我有两个下降。第一个加载第二个的数据。一旦用户在第二个下拉列表中选择一个选项,页面就会自动转发到该链接。
我试图从下拉菜单指向的网址中删除一些元素:
http://local.mysite/someplace/county /north-hempstead-ny/orange/gardnertown
我想要
我已经弄清楚了那个window.location.href.split('/')[2]
给了我我想要的。然而,下拉选择部分让我失望。
我认为这是需要修复的部分:
window.location.href.split('/')[2] + $places
.append("<option value=" + this.url + ">" + this.community + "</option>");
这是一个 小提琴。
感谢您的回复。我尝试添加代码,但它仍然没有删除额外的网址文本。控制台也不显示剥离。这是我修改后的代码:
document.getElementById('page-changer').reset();
$("#county").change(function() {
var $places = $("#township").empty();
$.each(data[$(this).val() - 1], function() {
$places.append("<option value=" + this.url + ">" + this.community + "</option>");
$('#jumpSubmit').click(function(e) {
e.preventDefault();
window.location = window.location.href.split('/')[2] + '/' + $('#township').val();
//console.log(window.location.href.split('/')[2]) + '/' + $('#township').val());
Good day, I have two drop downs. One loads in the data for the second. Once the user selects an option on the second dropdown, the page automatically forwards to that link.
I am trying to remove some elements from the url the drop down directs to:
http://local.mysite/someplace/county/north-hempstead-ny/orange/gardnertown
I want
I have already figured out thatwindow.location.href.split('/')[2]
gives me what I want. However the drop down selection part is throwing me off.
This is the part that needs fixing i think:
window.location.href.split('/')[2] + $places
.append("<option value=" + this.url + ">" + this.community + "</option>");
Here is a fiddle.
Thanks for the responses. I tried to add the code but it still doesn't strip out the extra url text. The console is not showing the stripping either. Here is my revised code:
document.getElementById('page-changer').reset();
$("#county").change(function() {
var $places = $("#township").empty();
$.each(data[$(this).val() - 1], function() {
$places.append("<option value=" + this.url + ">" + this.community + "</option>");
$('#jumpSubmit').click(function(e) {
e.preventDefault();
window.location = window.location.href.split('/')[2] + '/' + $('#township').val();
//console.log(window.location.href.split('/')[2]) + '/' + $('#township').val());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要
You just need the value from the
<select>
您可以使用 jQuery 根据您选择的元素构建您喜欢的 URL。例如:
更新了代码以使用现有 URL 与硬编码 URL。
You can use jQuery to build the URL however you like based on your select elements. For example:
Updated code to use existing URL vs hard coded URL.