简体   繁体   English

在Jquery中输入文本时,请更改锚标记中的文本

[英]Change text in the anchor tag while typing in text input in Jquery

Ok, this sounds easy, but I'm having problems trying to identify the anchor tag. 好的,这听起来很简单,但是我在尝试识别锚标记时遇到了问题。 The HTML block repeats itself. HTML块会重复自身。

<div class="panel-heading">
  <h4 class="panel-title">
    <a data-toggle="collapse" data-parent="#accordion" href="#q1">
     Question 1
    </a>
  </h4>
</div>
<div id="q1" class="panel-collapse collapse">
  <div class="panel-body">
    <div class="form-group">
      <label for="inputQ1" class="col-sm-2 control-label">Question</label>
      <div class="col-sm-7">
        <input type="text" class="form-control" id="inputQ1" placeholder="e.g How was our service?">
      </div>
    </div>
  </div>
</div>

I got the answer I need over here how to show text while typing in textarea in jquery 我得到了我在这里需要的答案如何在jquery中键入textarea时显示文本

What I need to know is, how do I make it so that the text in the anchor tag changes? 我需要知道的是,如何使锚标记中的文本更改? I don't want to put in an ID for all of them. 我不想为所有这些输入ID。 I should be using parent() and siblings(), right? 我应该使用parent()和siblings(),对吗?

Try 尝试

jQuery(function ($) {
    $('.panel-body input').keyup(function(){
        $(this).closest('.panel-collapse').prev('.panel-heading').find('a').text(this.value)
    })
});

Demo: Fiddle 演示: 小提琴

Note: To be more specific I might add an additional class as shown in this fiddle 注意:更具体地说,我可能会添加一个额外的类,如该小提琴所示

You can do this with keyup and html: 您可以使用keyup和html来做到这一点:

$(document).ready(function(){

    $("#text").keyup(function(){

        $("#q1").html($(this).val());

    });

});

Where #text is the textarea and #q1 is the anchor: 其中#text是文本区域,而#q1是锚点:

JSFiddle JSFiddle

You can use: 您可以使用:

$('#inputQ1').keyup(function(){
    var $this = $(this);
    $this.closest('#q1').prev().find('a').html($this.val());
});

Fiddle Demo 小提琴演示

I think the best (because the logic) if you could create a parent around this code (if you can) 我认为最好(因为逻辑),如果您可以围绕此代码创建一个父对象(如果可以)

<div class="parent">
  <h4>
    <a>...</a>
  </h4>
  <div>
     <input>
  </div>
</div>

in the event $(this) is your input 如果$(this)是您的输入

you can use $(this).closest('parent').find('a') to find the for the 您可以使用$(this).closest('parent').find('a')查找

尝试使用锚定父级:

$('.panel-title a').text( /* textarea text */ );

尝试这个

$('.panel-title a').text( 'new text' );

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

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