简体   繁体   English

在网址中添加任何点击链接中的文字

[英]Append URL with text from any clicked link

I have a table of part numbers. 我有零件编号表。 My goal is to have the user click on the part number and have that part number populate into a form field on a separate page. 我的目标是让用户单击零件号,并将该零件号填充到单独页面上的表单字段中。 I have successfully accomplished this by appending the current page's URL with the clicked link text and then extrapolating it from the URL to populate the form field. 通过将当前页面的URL附加有单击的链接文本,然后从URL推断出来以填充表单字段,我已经成功完成了此任务。 My issue is that I have only had success with inline "onclick" script. 我的问题是,内联“ onclick”脚本仅获得成功。 I have hundreds of part numbers and putting the script on each line isn't ideal (or proper I imagine). 我有数百个零件编号,并且在每行上放置脚本都不理想(我想像的是正确的)。 This is what I am currently doing to accomplish this task: 这是我目前正在完成的任务:

  onclick="location.href = $(this).attr('href')+ '?' +  $.trim($(this).text());return false"

How do I do this without the inline script? 没有内联脚本怎么办?

I'm cobbling a website together for my family's business. 我正在为一家人的生意一起整理一个网站。 I'm not a professional - so please forgive my ignorance. 我不是专业人士-请原谅我的无知。 I'm extremely grateful for any assistance. 我非常感谢您的协助。

If you don't want the script on each line in your javascript section you could add the following code: 如果您不希望JavaScript部分中的每一行都包含脚本,则可以添加以下代码:

$(function(){
    $('table a').on('click',function(){
      var url=$(this).attr('href')+'?'+$.trim($(this).text());
      $(this).attr('href',url);
    });
});

The jquery $('table a') is specific to only change the url if a in the table is clicked. jQuery $('table a')仅在单击表中的a时才更改url。 Then you can create the url from the current href and append what you want and reset the href. 然后,您可以从当前href创建URL,并添加所需内容并重置href。 However this will only change the current href of the element the user clicks and currently with return false you are stopping the user from progressing to the next page. 但是,这只会更改用户单击的元素的当前href ,并且当前使用return false会阻止用户继续进入下一页。

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

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