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

发布于 2024-11-30 03:15:12 字数 1704 浏览 0 评论 0原文

所以我已经研究了几个小时了,还没有弄清楚,我知道有一个简单的解决方案,所以我想我会在这里问。我正在尝试将文本区域的值传递到链接中作为 Twitter 共享按钮的一部分。我认为问题在于全局范围,我可以从外部 javascript 中获取变量,但不能从主 php 文件中获取变量。例如。

这是 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>

然后我在 extenral.js 中有 Javascript,它应该获取“friends_twitter”的值并将其发送回主页,这将允许我建立到该人的 Twitter 的动态链接。

我尝试了这个 var Friends_twitter_input = $("input[name=friends_twitter]"); 但如果我这样做 window.friends_twitter_input = $("input[ name=friends_twitter]"); 它工作得更好一些,但仍然没有达到预期。

我还编写了代码来检测关键更改,该更改有效,但只有当它放置在 external.js 内部时,

我可能会做所有错误的事情,但我似乎无法直接理解它。任何帮助都会很棒。

更新

我在 firebug 中收到错误“friends_twitter_input 未定义”,但不确定为什么

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();

    });
}

在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>

我认为我不需要编写共享链接,直到输入有值或将值定义为 null。

感谢您的所有帮助。

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. 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. For example.

Here is the 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.

I tried this var friends_twitter_input = $("input[name=friends_twitter]"); but got undefined if i do 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

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

inside 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

<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.

Thanks for all the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸢与 2024-12-07 03:15:12

不太确定所有代码的结构,但有一些可能有帮助的事情。

要从输入按钮获取值,这是您需要的代码。

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

还要确保在输入文本就位后执行尝试读取输入文本值的代码。

要在页面加载后执行 JavaScript,请使用以下命令:

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

要连接到输入文本的 oncahnge 事件,请执行此操作(在就绪处理程序内)

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

请注意,onchange 事件中的代码仅用于演示目的,您可能应该使用onblur 或更好地连接到提交按钮的 onclick 事件中。

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:

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

To hook up to the oncahnge event of the input text do this (inside the ready handler)

$("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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文