简体   繁体   中英

Jquery concat variable to string doesn't work

I got the value of the input element with id mydata into a variable 's' . When I concatenate this variable to another string , the resulting string doesn't have the variable in it.

$("#mydata").change(function () {
   var s =$("#mydata").val();
   var link = "<a href='{%url 'download' "+s+" %}' style='color:darkslategrey;'>View E-Brochure</a>";
});

Variable s has value 1 the resulting string is:

<a href="/download/%7B0%7D" style="color:darkslategrey;">View E-Brochure </a> 

but I'm expecting,

  <a href="/download/1" style="color:darkslategrey;">View E-Brochure </a> 

Your script is missing closing parentheses and a semi-colon.

 $("#mydata").change(function () {  
   var s = $("#mydata").val();
   var link = "<a href='{%url 'download' "+s+" %}' style='color:darkslategrey;'>View E-Brochure</a>";
   alert(link);
 });

working link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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