jQuery函数keyup在我的项目上不起​​作用,其他函数起作用

时间:2018-09-04 14:49:26

标签: javascript jquery

我不知道如果我输入输入show的值的任何字符,为什么此代码不起作用

 <div>
   <input class="form-control live" name="name" placeholder="Item Name" 
            data-class="live-title" required/>
 </div>

 $('.live').keyup(function () {
    $($(this).data('class')).text($(this).val());
 });

 <div class="card cats">
   <span class='price-tag'>$<span class="live-price">0</span></span>
   <img class='' src='' alt=''/>
   <div class='card-body'>
       <h5 class='live-title'>title</h5>
       <p class='live-desc'>Description</p>
       <p class='live-country'>Country</p>
   </div>
 </div>     

1 个答案:

答案 0 :(得分:2)

$(this).data('class')将是live-title。因此,要选择具有此类的dom,您需要使用点(.)为其添加类选择器前缀

$('.live').keyup(function() {
  $("." + $(this).data('class')).text($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input class="form-control live" name="name" placeholder="Item Name" data-class="live-title" required/>
</div>


<div class="card cats">
  <span class='price-tag'>$<span class="live-price">0</span></span>
  <img class='' src='' alt='' />
  <div class='card-body'>
    <h5 class='live-title'>title</h5>
    <p class='live-desc'>Description</p>
    <p class='live-country'>Country</p>
  </div>
</div>