如何创建“链接"单击时在输入字段中输入特定的关键字? [英] How can I create a "link" to input particular keyword into input field when it's clicked?

查看:105
本文介绍了如何创建“链接"单击时在输入字段中输入特定的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Rails应用程序.
如果单击该按钮,它将自动在body_input
中输入@keyword 我希望使用link_to而不是button_to.
我该怎么办?

I'm working on Rails application.
If you click on that button, it automatically input @keyword into body_input
I want it with link_to instead of button_to.
How can I?

查看

<%= button_to "Populate the chat box", "#name", :id => :username, :value => @keyword %>

jQuery

$(document).ready(function(e) {
     $('button#username').click(function () {
         e.preventDefault();
         $(".chat_input#body_input").val($(this).attr('value'));
     });
});

其他信息(这是HTML输出的javascript)

Additional Info(This is output javascript in HTML)

<script type="text/javascript">
    //<![CDATA[     
        $(document).ready(function(e) {
             $('button#username').click(function () {
                 e.preventDefault();
                 $(".chat_input#body_input").val($(this).attr('value'));
             });
        });
    //]]>
</script>

推荐答案

以下两个在功能上是等效的,但在标记中是等效的:

The following two are equivalent in function, though not in markup:

<%= link_to "Populate the chat box", "#name", :id => :username, :value => @keyword %>
<%= button_to "Populate the chat box", "#name", :id => :username, :value => @keyword %>

正如评论者 PSL 所指出的那样,如果您决定使用link_to跳转到命名锚点,您需要修改jQuery函数以删除e.preventDefault():

As commenter PSL astutely points out, if you decide to utilize a link_to to jump to a named anchor, you'll need to modify your jQuery function to remove e.preventDefault():

$(document).ready(function() {
     $('a#username').click(function () {
         $(".chat_input#body_input").val($(this).attr('value'));
     });
});

这篇关于如何创建“链接"单击时在输入字段中输入特定的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆