如何在下拉菜单中更换链接的某个部分?

时间:2015-08-23 00:11:19

标签: javascript jquery

当我更改下拉菜单值时,我需要一些javascript来帮助替换链接的某个部分。 我想通过javascript onchange of dropdown menue替换部分链接。因此,当我更改下拉菜单时,我想通过 var e 更改下面链接中的数字theTotal = 12

<a class="btn_full" href="/traveltour/index.php/paypal/buy?theTotal=12&amp;description=Pinko+Pallino+&amp;quantity=1&amp;tour_id=6&amp;tourextra_id=11&amp;guide_id=35">Go to payment</a>

 $(document).ready(function () {


        $("#Toursextra_count").on("change", function () {

            var a = 22;
            var b = $(this).find("option:selected").attr("value") * a;
            var c = $(this).find("option:selected").attr("value");
            var d = (b*0.10).toFixed(2);
            var e = parseFloat(d)+parseFloat(b);
            $(".totalcash ").text(b);
            $(".adults").text(c);
            $(".comission").text(d);
            $(".grantotal").text(e);
        });
    });

1 个答案:

答案 0 :(得分:1)

您可以.replace()使用regular expression来操纵链接的href属性,如下所示:

jsFiddle

&#13;
&#13;
╔══════╦════════════╦════════════╗
║ Item ║  Weekend   ║ Week_Total ║
╠══════╬════════════╬════════════╣
║    1 ║ 2015-01-03 ║         92 ║
║    2 ║ 2015-01-03 ║         95 ║
║    1 ║ 2015-06-27 ║          4 ║
║    2 ║ 2015-06-27 ║         66 ║
║    1 ║ 2015-07-04 ║         12 ║
║    2 ║ 2015-07-04 ║         12 ║
╚══════╩════════════╩════════════╝
&#13;
 $(document).ready(function () {
        $("#Toursextra_count").on("change", function () {

            var a = 22;
            var b = $(this).val() * a; 
            var c = $(this).val();
            var d = (b*0.10).toFixed(2);
            var e = parseFloat(d)+parseFloat(b);
            $(".totalcash ").text(b);
            $(".adults").text(c);
            $(".comission").text(d);
            $(".grantotal").text(e);
            var url = $('#myBtn').attr('href');
            var newSection = 'theTotal='+e+'&'
            url = url.replace(/theTotal=[\d.]+&/,newSection)
            $('#myBtn').attr('href',url);
        });
    });
&#13;
&#13;
&#13;