[英]Change text in the anchor tag while typing in text input in Jquery
好的,這聽起來很簡單,但是我在嘗試識別錨標記時遇到了問題。 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>
我得到了我在這里需要的答案如何在jquery中鍵入textarea時顯示文本
我需要知道的是,如何使錨標記中的文本更改? 我不想為所有這些輸入ID。 我應該使用parent()和siblings(),對嗎?
您可以使用keyup和html來做到這一點:
$(document).ready(function(){
$("#text").keyup(function(){
$("#q1").html($(this).val());
});
});
其中#text
是文本區域,而#q1
是錨點:
您可以使用:
$('#inputQ1').keyup(function(){
var $this = $(this);
$this.closest('#q1').prev().find('a').html($this.val());
});
我認為最好(因為邏輯),如果您可以圍繞此代碼創建一個父對象(如果可以)
<div class="parent">
<h4>
<a>...</a>
</h4>
<div>
<input>
</div>
</div>
如果$(this)
是您的輸入
您可以使用$(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.