开发者

Jquery select href build

开发者 https://www.devze.com 2023-03-08 02:47 出处:网络
Fairly new to jquery so I might be making a simple mistake but I just cant seem to find it. At the moment I have a select drop down that I want to send the user to the url that is built from the optio

Fairly new to jquery so I might be making a simple mistake but I just cant seem to find it. At the moment I have a select drop down that I want to send the user to the url that is built from the option they choose. At the moment开发者_开发百科 I have this for my jquery:

    $('#sizeSelect').change(function () {
       var singleValue = $(this).find('option:selected').val();
       var singleUrl = '/products/length' + '~' + escape(singleValue);
       alert(escape(singleValue));
       alert(singleUrl);
       alert('/products/length' + '~' + escape(singleValue));
    window.location = $(this).find('option:selected').attr('href', 'singleUrl');
});

I have my alerts in there for me to see how everything was being put together. When I get to my actual window.location the url that is built has [object%20Object] in it . So any help would much appreciated to a a somewhat new jquery user.


Why not send your users directly to the singleUrl variable?

window.location = singleUrl;

I'm not exactly sure what you're trying to do on the last line:

$(this).find('option:selected') will return the selected li element, but it will be returned as an object. You will need to use .val() as you did earlier to return the value of the li.

The second part, .attr('href', 'singleUrl') will attempt to find the href attribute of the li element (which doesn't exist) and set its value to singleUrl, which really won't do anything for you.


singleUrl is a variable, not a string. Change to:

window.location = $(this).find('option:selected').attr('href', singleUrl);
0

精彩评论

暂无评论...
验证码 换一张
取 消