简体   繁体   English

Javascript:使用Jquery在父输入内部键入时更新标签内容

[英]Javascript: Update label content while typing inside of a parent input with Jquery

How to update with jquery the label of preview div when im typing inside of the parent input of edit div? 当我在编辑div的父输入里面输入时,如何使用jquery更新预览div的标签? Thank you. 谢谢。

<html>
        <body>
                <div id="preview">
                        <label id="companyName" class="workExperience">
                                This is my company
                        </label>
                </div>
                <div id="edit">
                        <label>Company Name: </label>
                        <input type="text" id="companyName" />
                </div>
        </body>
</html>

You'd have to have something along these lines in your code: 您必须在代码中包含以下内容:

<script>
$("#companyName").keyup(function () {
  var value = $(this).val();
  $(".workExperience").text(value);
}).keyup();
</script>

However, I would NOT give them the same id value, as it might lead to some errors. 但是,我不会给它们相同的id值,因为它可能会导致一些错误。

Good luck! 祝好运!

you can not have the same ID in the tag "label" and the tag "input". 您不能在标签“label”和标签“input”中使用相同的ID。

<div id="preview">
  <label id="companyName" class="workExperience">
                                This is my company
  </label>
</div>
<div id="edit">
  <label>Company Name: </label>
  <input type="text" id="companyNameInput" />
</div>

$(document).ready(function(e){

    $('#companyNameInput').bind('change', function(ev) {

        $(this).closest('#edit').prev().find('#companyName').html($(this).val());

    });


});
​

test 测试

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

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