Fill in Input Field by Clicking Link with JQuery

前端 未结 3 484
予麋鹿
予麋鹿 2021-01-15 03:59

How might someone fill in an input field with some text using jquery by clicking that text?

The text is generated dynamically so the value is not know ahead of time.

相关标签:
3条回答
  • 2021-01-15 04:34

    Try this:

    $(".special_field_link").click(function(){
     $("#a_input_id").val($(this).text());
    });
    
    0 讨论(0)
  • 2021-01-15 04:44

    HTML:

    <input id="a_input_id" type="text">
    <a href="#" class="special_field_link">@r.SpecialField<a>
    

    JS:

    $(function(){
        $('.special_field_link').live('click', function() {
            $("#a_input_id").val($(this).html());
        });
    });
    

    Also, you can use .text() instead of .html() as listed in this answer: Fill in Input Field by Clicking Link with JQuery

    Running Sample: http://fiddle.jshell.net/s8nUh/

    0 讨论(0)
  • 2021-01-15 04:45
    $('a.special_field_link').live('click', function(e){
        $('#a_input_id').val(this.innerHTML);
        return false; // to prevent default
    });
    
    0 讨论(0)
提交回复
热议问题