简体   繁体   English

jQuery 将文本添加到单击时输入

[英]jQuery add text to input on click

I am trying to use javascript to have a link created on page load and then when that link is clicked to insert content into a div.我正在尝试使用 javascript 在页面加载时创建一个链接,然后在单击该链接以将内容插入 div 时。

This is where I'm at so far...这是我目前为止...

<div class="inside">
</div>

<div class="content">
<input type="textarea">
</div>

<script>
jQuery( document ).ready(function() {
   jQuery( ".inside" ).append( '<p><a href="">Insert Content</a></p>' );
});
</script>

Fiddle: https://jsfiddle.net/03afj8fd/小提琴: https : //jsfiddle.net/03afj8fd/

Now I need to be able to insert some preset content into the input box when the generated link is clicked.现在我需要能够在单击生成的链接时将一些预设内容插入到输入框中。 Anyone point me in the right direction of an example?有人指出我正确的示例方向吗?

Here is an example.这是一个例子。 Also there is no type textarea .也没有类型textarea textarea is the element tag. textarea是元素标签。

Fiddle: https://jsfiddle.net/AtheistP3ace/03afj8fd/3/小提琴: https : //jsfiddle.net/AtheistP3ace/03afj8fd/3/

HTML: HTML:

<div class="inside"></div>
<div class="content">
    <textarea></textarea>
</div>

JS: JS:

jQuery(document).ready(function () {
    $(".inside").append('<p><a id="myLink" href="#">Insert Content</a></p>');
    $('#myLink').on('click',
        function () {
            $('textarea').val('some content');
        }
    );
});

You can put the preset content in the a element so that if you have so many such elements then it would be flexible as each element will have it's own preset content.您可以将预设内容放在a元素中,这样如果您有这么多这样的元素,那么它会很灵活,因为每个元素都有自己的预设内容。

 jQuery( document ).ready(function() { jQuery( ".inside" ).append( '<p><a href="" data-preset="Preset Content">Insert Content</a></p>' ) //set up an event listner for the a element .on('click', 'p > a', function( e ) { e.preventDefault(); jQuery('.content > textarea').val( $(this).data('preset') ); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="inside"></div> <div class="content"><textarea></textarea></div>

jQuery( document ).ready(function() {
   jQuery( ".inside" ).append( '<p><a href="#" id="samplelink">Insert Content</a></p>' );
});

Now you need to define .on for the a#samplelink as it was dynamically added to the DOM.现在您需要为a#samplelink定义.on ,因为它是动态添加到 DOM 的。 ( .click() won't work) .click()不起作用)

jQuery(document).on("click", "a#samplelink", function(e){
    e.preventDefault(); // To prevent redirection from link
    $("div.content input").val("Sample Preset Content"); // Setting value of input
})

You can do something like this:你可以这样做:

jQuery( document ).ready(function() {
   jQuery( ".inside" ).append( 
        jQuery('<p />').append(
            jQuery('<a href="">Insert Content</a>').on('click', function(e) {
                e.preventDefault();
                jQuery('input').val('hi');
            }) 
        )
    );
});

you'll want to give the link and input a class or id, and delegate an event to it.您需要提供链接并输入一个类或 ID,然后将一个事件委托给它。

'<p><a class='content' href="">Insert Content</a></p>'
<input class='context-input' type="textarea" />

and in your script并在您的脚本中

jQuery('.inside').on('click', '.content', function(){
  jQuery('.context-input').val('Your Presets')
})

what this did was create a delegated event so that dynamic content within .inside that has the class .content will trigger on click.这所做的是创建一个委托事件,以便.inside中具有类.content动态内容将在点击时触发。 this step is necessary because the .content div is not present when the origin script is loaded, and thus needs a delegated listener, instead of a direct one这一步是必要的,因为加载原始脚本时.content div 不存在,因此需要委托的侦听器,而不是直接的侦听器

I updated the fiddle with an example solution.我用示例解决方案更新了小提琴。

You can find it here: https://jsfiddle.net/03afj8fd/11/你可以在这里找到它: https : //jsfiddle.net/03afj8fd/11/

jQuery( document ).ready(function($) {
   $clickMe = $('<span>Insert Content</span>');

   $clickMe.on('click', function(e) {
      e.preventDefault();
      $('#fillMe').val('test');  
   });

   $( ".inside" ).append($clickMe);
});

There are multiple ways how you can achieve this though.有多种方法可以实现这一目标。

I'm not sure if you need the "a" tag or if you just added it because you thought it would be needed to have it work as a clickable element - i removed it in my fiddle.我不确定您是否需要“a”标签,或者您是否只是添加了它,因为您认为需要它作为可点击元素工作 - 我在我的小提琴中删除了它。

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

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