如何使用jQuery锚点设置文本框的值?

时间:2022-02-23 07:50:09

I have a textbox whose value I want to set based on the inner text of an anchor tag. In other words, when someone clicks on this anchor:

我有一个文本框,它的值是基于锚标记的内部文本来设置的。换句话说,当有人点击这个锚时:

<a href="javascript:void();" class="clickable">Blah</a>

I want my textbox to populate with the text "Blah". Here is the code I am currently using:

我想让我的文本框填充文本“Blah”。以下是我目前使用的代码:

<script type="text/javascript">
    $(document).ready(function(){
        $("a.clickable").click(function(event){
            $("input#textbox").val($(this).html());
        });  
    });
</script>

And in my html there is a list of anchor tags with the class "clickable" and one textbox with the id "textbox".

在我的html中有一个锚标签列表,类为“可点击”,一个文本框id为“textbox”。

I've substituted the code above with code to just show a javascript alert with $(this).html() and it seems to show the correct value. I have also changed the $(this).html() to be a hardcoded string and it setes the textbox value correctly. But when I combine them the textbox simply clears out. What am I doing wrong?

我用代码替换了上面的代码,只显示了$(this).html()的javascript警报,它似乎显示了正确的值。我还将$(this).html()更改为硬编码字符串,并正确地设置文本框值。但当我合并它们时,文本框就会消失。我做错了什么?

5 个解决方案

#1


52  

I wrote this code snippet and it works fine:

我写了这个代码片段,它运行得很好:

<a href="#" class="clickable">Blah</a>
<input id="textbox">
<script type="text/javascript">
    $(document).ready(function(){
        $("a.clickable").click(function(event){
            event.preventDefault();
            $("input#textbox").val($(this).html());
        });  
    });
</script>

Maybe you forgot to give a class name "clickable" to your links?

也许你忘记给你的链接一个类名“点击”?

#2


14  

To assign value of a text box whose id is ёtextboxё in jQuery please do the following

指定值的一个文本框的idёtextboxё在jQuery请以下

$("#textbox").val('Blah');

#3


6  

Just to note that prefixing the tagName in a selector is slower than just using the id. In your case jQuery will get all the inputs rather than just using the getElementById. Just use $('#textbox')

需要注意的是,在选择器中前缀tagName比只使用id要慢,在您的示例中,jQuery将获得所有输入,而不仅仅是使用getElementById。只使用$(“#文本框”)

#4


4  

To assign value of a text box whose id is "textbox" in JQuery please do the following

要分配id为“textbox”的JQuery文本框的值,请执行以下操作

$("#textbox").get(0).value = "blah"

$(" #文本框"). get(0)。值=“废话”

#5


1  

Following redsquare: You should not use in href attribute javascript code like "javascript:void();" - it is wrong. Better use for example href="#" and then in Your event handler as a last command: "return false;". And even better - use in href correct link - if user have javascript disabled, web browser follows the link - in this case Your webpage should reload with input filled with value of that link.

redsquare:您不应该在href属性javascript代码中使用“javascript:void()”;——这是错误的。最好使用href="#",然后在事件处理程序中作为最后一个命令:"return false;"。更好的是——在href正确的链接中使用——如果用户禁用了javascript,那么web浏览器就会跟随该链接——在这种情况下,你的网页应该重新加载填满该链接的值的输入。

#1


52  

I wrote this code snippet and it works fine:

我写了这个代码片段,它运行得很好:

<a href="#" class="clickable">Blah</a>
<input id="textbox">
<script type="text/javascript">
    $(document).ready(function(){
        $("a.clickable").click(function(event){
            event.preventDefault();
            $("input#textbox").val($(this).html());
        });  
    });
</script>

Maybe you forgot to give a class name "clickable" to your links?

也许你忘记给你的链接一个类名“点击”?

#2


14  

To assign value of a text box whose id is ёtextboxё in jQuery please do the following

指定值的一个文本框的idёtextboxё在jQuery请以下

$("#textbox").val('Blah');

#3


6  

Just to note that prefixing the tagName in a selector is slower than just using the id. In your case jQuery will get all the inputs rather than just using the getElementById. Just use $('#textbox')

需要注意的是,在选择器中前缀tagName比只使用id要慢,在您的示例中,jQuery将获得所有输入,而不仅仅是使用getElementById。只使用$(“#文本框”)

#4


4  

To assign value of a text box whose id is "textbox" in JQuery please do the following

要分配id为“textbox”的JQuery文本框的值,请执行以下操作

$("#textbox").get(0).value = "blah"

$(" #文本框"). get(0)。值=“废话”

#5


1  

Following redsquare: You should not use in href attribute javascript code like "javascript:void();" - it is wrong. Better use for example href="#" and then in Your event handler as a last command: "return false;". And even better - use in href correct link - if user have javascript disabled, web browser follows the link - in this case Your webpage should reload with input filled with value of that link.

redsquare:您不应该在href属性javascript代码中使用“javascript:void()”;——这是错误的。最好使用href="#",然后在事件处理程序中作为最后一个命令:"return false;"。更好的是——在href正确的链接中使用——如果用户禁用了javascript,那么web浏览器就会跟随该链接——在这种情况下,你的网页应该重新加载填满该链接的值的输入。