使用Jquery从动态链接下拉列表中取出url信息

发布于 2025-01-08 03:26:42 字数 1491 浏览 0 评论 0原文

美好的一天,我有两个下降。第一个加载第二个的数据。一旦用户在第二个下拉列表中选择一个选项,页面就会自动转发到该链接。

我试图从下拉菜单指向的网址中删除一些元素:

http://local.mysite/someplace/county /north-hempstead-ny/orange/gardnertown

我想要

http://local.mysite/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

http://local.mysite/orange/gardnertown

I have already figured out that
window.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别理我 2025-01-15 03:26:42

您只需要

var $full_url = $("#your_select").val();
var $part_you_need = $full_url.split('/')[2];
alert( $part_you_need );

You just need the value from the <select>

var $full_url = $("#your_select").val();
var $part_you_need = $full_url.split('/')[2];
alert( $part_you_need );
倾其所爱 2025-01-15 03:26:42

您可以使用 jQuery 根据您选择的元素构建您喜欢的 URL。例如:

$('#jumpSubmit').click(function(e) {
    e.preventDefault();
    window.location = window.location.href.split('/')[2] + '/' + $('#township').val();
    //console.log(window.location.href.split('/')[2] + '/' + $('#township').val());
});​
​

更新了代码以使用现有 URL 与硬编码 URL。

You can use jQuery to build the URL however you like based on your select elements. For example:

$('#jumpSubmit').click(function(e) {
    e.preventDefault();
    window.location = window.location.href.split('/')[2] + '/' + $('#township').val();
    //console.log(window.location.href.split('/')[2] + '/' + $('#township').val());
});​
​

Updated code to use existing URL vs hard coded URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文