开发者

changing href doesn't work

开发者 https://www.devze.com 2023-02-16 18:11 出处:网络
i like to add query string dynamically in in url. the whole table is from jquery Datatable when i change select box(on change) ,

i like to add query string dynamically in in url. the whole table is from jquery Datatable

when i change select box(on change) , i need to add this value to all anchors in td.so i did that, through following code. when i alert href i getting what i want

("XXX?YY=YY&bsort=ZZ")

. but the anchor href's doesn't have that (&bsort=XX) query string(checking by fire bug) whats wrong ?

mycode

$('select[name="category_table_length"]').change(function(){
    var b,href;
    b = $(this).val();
        $('td a').each(function(){
        href = $(this).attr('href');
        href = href+"&bsort="+b;开发者_JAVA百科 

        $(this).attr('href',href);
        });
});


It works for me: http://jsfiddle.net/7QTzt/

Is your HTML valid?

BTW, your code doesn't take into account having to remove a "&bsort" property when you select from the dropdown a second time. Perhaps consider a more sophisticated approach.


try this example:

HTML:

<select name="category_table_length">
    <option value="1">sort 1</option>
    <option value="2">sort 2</option>
    <option value="3">sort 3</option>    
</select>

<table id="mytable">
    <tr>
        <td><a href="/page.php?data=a">anchor 1</a></td>
    </tr>
    <tr>
        <td><a href="/page.php?data=a">anchor 2</a></td>
    </tr>
</table>

JS:

$('select[name="category_table_length"]').change(function(event){

    var 
        actual_value = $(this).val(),
        new_href = "";

    $('#mytable tr td a').each(function(i, el){

        if (!$(el).attr("original_href"))
            $(el).attr("original_href", $(el).attr('href') )

        new_href = $(el).attr("original_href") + "&bsort=" + actual_value;

        $(el)
            .attr('href', new_href)
            .html("change " + new_href);

    });
});

if you do not have "?" should change to:

new_href = $(el).attr("original_href") + "?bsort=" + actual_value;
0

精彩评论

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