简体   繁体   English

如何在href链接后附加一个select标签值

[英]How to append a href link with a select tag value

I am attempting to change a href link based on two different select tags changed 我正在尝试根据已更改的两个不同的选择标签来更改href链接

So far I have got it to append with 1 select tag, however i cannot get it to work with the other. 到目前为止,我已经添加了一个select标签,但是我无法使其与其他标签一起使用。 Any help or suggestions would be appreciated. 任何帮助或建议,将不胜感激。 Thanks 谢谢

$('#to').on('change', function() {
    var value2 = $(this).val();
});

var base_href="/converter/";

$('#from').on('change', function() {
    var value=$(this).val();

    if (base_href=="")
       base_href = $("#cch-link").attr("href");

    $("#cch-link").attr("href",base_href+value+'-to-'+value2);
});

You are defining the value2 variable in the context of the first handler, outside of that context the variable is undefined , you can define a global variable or: 您要在第一个处理程序的上下文中定义value2变量,在该上下文之外,该变量是undefined ,您可以定义一个全局变量或:

var base_href = "/converter/";

function setHref() 
{
   $("#cch-link").attr('href', function() 
   {
       var to = $('#to').val();           
       var from = $('#from').val();

       return base_href + from + '-to-' + to;
   });
}

$('#to, #from').on('change', setHref); // .change();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM