简体   繁体   English

通过JavaScript将动态文本输入传递到链接中

[英]Passing a dynamic text input into a link via javascript

So i've been at this for hours and havn't figure it out and I know there is an easy solution so I figured I would ask here. 所以我已经待了几个小时了,还没有弄清楚,我知道有一个简单的解决方案,所以我想问一下。 I am trying to pass the value of a text area into a link as part of a twitter share button. 我正在尝试将文本区域的值传递到链接中,作为Twitter共享按钮的一部分。 I think the problem is with global scopes I am able to get the variable from within my external javascript but not from my main php file. 我认为问题在于全局范围,我能够从外部javascript中获取变量,但无法从主要php文件中获取变量。 For example. 例如。

Here is the html: 这是html:

   <div id="share_twitter">
    Friends Twitter Username (optional) <input type="text" name="friends_twitter" value="@" />
<script type="text/javascript"> 
       window.document.write('<a href="http://twitter.com/share?text='+window.twitter_text+'&url=http://example.com/<?php echo $slug;?>" class="twitter-share-button"></a>'); 
</script>

Then I have the Javascript in extenral.js which is suppose to get the value of "friends_twitter" and send it back to the main page which will allow me to make a dynamic link to that persons twitter. 然后我在extenral.js中有了Javascript,该Javascript可能是为了获取“ friends_twitter”的值并将其发送回主页,这使我可以动态链接到该人的twitter。

I tried this var friends_twitter_input = $("input[name=friends_twitter]"); 我试过这个var friends_twitter_input = $("input[name=friends_twitter]"); but got undefined if i do window.friends_twitter_input = $("input[name=friends_twitter]"); 但是如果我做window.friends_twitter_input = $("input[name=friends_twitter]");却不undefined window.friends_twitter_input = $("input[name=friends_twitter]"); it works a bit better but still not as intended. 它的工作效果更好,但仍未达到预期的效果。

I also wrote code to detect key change which works but only if it's placed inside of external.js 我还编写了代码来检测有效的键更改,但前提是将其放置在external.js中

I may be going about this all wrong but I can't seem to get it staight. 我可能会把这一切都弄错了,但是我似乎无法保持稳定。 Anyhelp would be great. 任何帮助将是巨大的。

Update 更新资料

I'm getting the error "friends_twitter_input is not defined" in firebug but not sure why 我在萤火虫中收到错误“ friends_twitter_input未定义”,但不确定为什么

inside custom.js 在custom.js内部

$(document).ready(function(){
var friends_twitter_input = $("input[name=friends_twitter]").val();
    $(".twitter-share-button").click(function() {
            var friends_twitter_input = $('input[name=friends_twitter]').val();

    });
}

inside index.php 内部index.php

<script type="text/javascript"> 
    window.document.write('<a href="http://twitter.com/share?text='+friends_twitter_input+'&url=http://example.com/<?php echo $slug;?>" class="twitter-share-button"></a>');
 </script>

I think i need to not write the share link until the input has a value or define the value as null. 我认为我不需要在输入具有值或将值定义为null之前编写共享链接。

Thanks for all the help. 感谢您的所有帮助。

Not quite sure about the structure of all your code but a few things that may help. 不太确定所有代码的结构,但可能会有一些帮助。

To get the value from the input button this is the code you need. 要从输入按钮获取值,这是您需要的代码。

var friends_twitter_input = $("input[name=friends_twitter]").val();

Also make sure that the code that tries to read the value of the input text is executed after the input text is in place. 此外,请确保在输入文本到位后执行尝试读取输入文本值的代码。

To execute your javascript after the page load use this: 要在页面加载后执行JavaScript,请使用以下命令:

$(document).ready(function() {
  //All your javascript here.
});

To hook up to the oncahnge event of the input text do this (inside the ready handler) 要连接到输入文本的oncahnge事件,请执行此操作(在ready处理程序内部)

$("input[name=friends_twitter]").onchange(function() {
     var friends_twitter_input = $(this).val();
});

Please note that the code in the onchange event is just for demo purposes you should probably want to use onblur or better hook up in the onclick event of the submit button. 请注意,onchange事件中的代码仅用于演示目的,您可能应该使用onblur或更好地关联提交按钮的onclick事件。

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

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