Javascript 如何使用href链接将javascript变量作为参数传递?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25861008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:57:47  来源:igfitidea点击:

How to pass javascript variables as parameters with a href link?

javascripthtml

提问by developer82

I have a JavaScript variable which I wan to pass along with an HTML link.

我有一个 JavaScript 变量,我想将它与 HTML 链接一起传递。

<script>
var demo=10;
</script>

I get the value of demo on executing one javascript function & few if-else & for loops. Since that code doesn't make any sense to this question, I haven't included that. Assume after all those operations, the final result that I get is stored in the demo variable. Now I wanna pass this demo variable along with an link.

我在执行一个 javascript 函数和几个 if-else 和 for 循环时获得了演示的价值。由于该代码对这个问题没有任何意义,因此我没有包含它。假设在所有这些操作之后,我得到的最终结果存储在 demo 变量中。现在我想传递这个演示变量和一个链接。

<a href="to_fake_page.php">On click pass demo</a>

I am trying to pass the demo variable as a parameter to the href link. Is this ever possible???

我试图将演示变量作为参数传递给 href 链接。这有可能吗???

I know

我知道

window.location.href = "to_fake_page.php?demostore=demo";

would do the same.

也会这样做。

UPDATE

更新

This is my Javascript function

这是我的 Javascript 函数

This function has one parameter& it is passed onto a_php_page.phpwhere some database operations & condition checks are performed & the corresponding result is passed back from the PHP page to the AJAX function as JSON. I parse the JSON & obtain demovariable. And a HTML modal is included if I get response from the PHP page. Inside the HTML modal, I have a link pointing to_fake_page.phpto where I have to pass the demo variable on clicking a link On click pass demo.

这个函数有一个parameter& 它被传递到执行a_php_page.php一些数据库操作和条件检查的地方 & 相应的结果从 PHP 页面作为 JSON 传递回 AJAX 函数。我解析 JSON 并获取demo变量。如果我从 PHP 页面得到响应,则包含一个 HTML 模式。在 HTML 模式中,我有一个链接指向to_fake_page.php我必须在单击链接时传递演示变量的位置On click pass demo

 function onefunction(parameter)
   {
       if(window.XMLHttpRequest)
       {
           xmlhttp=new XMLHttpRequest();
       }
       else
       {
           xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
       xmlhttp.onreadystatechange=function() //callback fn
       {
           if(xmlhttp.readyState==4 && xmlhttp.status==200)
           {
               // these values are obtained from a php page using AJAX.
               // An array has been passed from the php page after json encoding it.

               var jsonStr = JSON.parse(xmlhttp.responseText);
               var demo=jsonStr.value1;
               document.getElementById('myLink').href += '?demo=' + encodeURIComponent(demo);

               $('<div class="modal fade">' +
                   '<div class="modal-dialog">' +
                   '<div class="modal-content">' +
                   demo+
                   '</div>' +
                   '<div class="modal-footer">' +
                   '<a href="to_fake_page.php" id="myLink">On click pass demo</a>' +
                   '</div>' +
                   '</div>' +
                   '</div>' +
                   '</div>').modal();

           }
       }
       xmlhttp.open("GET","a_php_page.php?from="+parameter,true);
       xmlhttp.send();
   }

回答by developer82

what you would have to do (after all your logic is done, and you have the value you want inside the variable) is to change the href of the link.

您必须做的(在完成所有逻辑之后,并且您在变量中拥有所需的值)是更改链接的 href。

something like this:

像这样:

var demo = 123;
document.getElementById('myLink').setAttribute('href', 'somelink.php?demo=' + demo);

also put example code here for you:

还将示例代码放在这里:

http://jsfiddle.net/x8tgktv6/

http://jsfiddle.net/x8tgktv6/

回答by developer82

To get the result I modified my JavaScript code like this:

为了得到结果,我修改了我的 JavaScript 代码,如下所示:

<script>
var demo=10;
var urll='to_fake_page.php?demostore='+demo;
</script>

and changed the line

并改变了线路

'<a href="to_fake_page.php" id="myLink">On click pass demo</a>' + 

in AJAX function to

在 AJAX 函数中

'<a href="'+url+'"' +
'id="myLink">On click pass demo</a>' +

It was simple as that. Added the variable urllto the DOM.

就这么简单。将变量添加urll到 DOM。

回答by Manish Sharma

try this,

尝试这个,

<script>
     var demo = 10;
     window.location.href = "to_fake_page.php?demostore=" + demo;
</script>

you want to set a tag href using this function.

您想使用此功能设置标签 href。

<script>    
   var link = document.getElementById('aTagId');
   link.setAttribute('href', 'to_fake_page.php?demostore=demo' + demo);
</script>