首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JQuery复制HTML并在添加到html主体之前编辑其内容。

JQuery复制HTML并在添加到html主体之前编辑其内容。
EN

Stack Overflow用户
提问于 2022-02-11 13:38:35
回答 1查看 30关注 0票数 0

我有一个包含如下输入字段的输入字段:

代码语言:javascript
复制
 <div class="px-4 py-6 mb-4 border rounded-lg border-gray-400" id="datarow"

<div class="flex flex-row space-x-4 pb-5">
    <div class="relative z-0 w-full mb-5">
        <input type="text" id="f_name" name="f_name" placeholder="Enter Name here"
            required class="pt-3 pb-2 block w-full px-4 mt-0 rounded bg-white border-0 border-b appearance-none" />
        <label for="f_name" class="absolute duration-300 pl-2 text-lg top-3 z-1 origin-0 text-gray-500">Name</label>
    </div>
    
    <div class="flex z-0 w-full justify-end">
        <input type="text" id="f_dest" name="f_dest" placeholder="Enter Destination here"
            required class="pt-3 pb-2 block w-full px-4 mt-0 rounded bg-white border-0 border-b appearance-none" />
        <label for="f_dest" class="absolute duration-300 pl-2 text-lg top-3 z-1 origin-0 text-gray-500">Destination</label>
    </div>
</div>

在jQuery中,我复制上面的div (按一下按钮),并附加到主html主体上,它将显示在原来的#datarow div下面。以下是我在程序中的全部代码片段。

代码语言:javascript
复制
$("#btn_addsector").click(function () {
    var div = document.getElementById("datarow"),
        clone = div.cloneNode(true);
 
     //neither of the lines work
     $(clone).find("#f_name").text = "Tisha";
    $("#maincontent").append(clone);
    $(clone).find("#f_name").text = "Tisha";
    $(clone).find("#f_name").text("Tisha");
    });
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="maincontent" >
<button id="btn_addsector"
        class="bg-transparent hover:bg-secondary-dark text-secondary-dark font-semibold hover:text-white py-2 px-4 border border-secondary-dark hover:border-transparent rounded">
        Add Sector
    </button>
 
 <div class="px-4 py-6 mb-4 border rounded-lg border-gray-400" id="datarow">

    <div class="flex flex-row space-x-4 pb-5">
        <div class="relative z-0 w-full mb-5">
            <input type="text" id="f_name" name="f_name" placeholder="Enter Name here" value="Hannah"
                required class="pt-3 pb-2 block w-full px-4 mt-0 rounded bg-white border-0 border-b appearance-none" />
            <label for="f_name" class="absolute duration-300 pl-2 text-lg top-3 z-1 origin-0 text-gray-500">Name</label>
        </div>
        
        <div class="flex z-0 w-full justify-end">
            <input type="text" id="f_dest" name="f_dest" placeholder="Enter Destination here" value="Smallville"
                required class="pt-3 pb-2 block w-full px-4 mt-0 rounded bg-white border-0 border-b appearance-none" />
            <label for="f_dest" class="absolute duration-300 pl-2 text-lg top-3 z-1 origin-0 text-gray-500">Destination</label>
        </div>
    </div>
</div>

</div>

我可以让克隆的div正确地附加,但是它不会改变输入字段的文本。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-11 13:43:30

您的代码有多个问题:

1:不能有多个ID相同的元素,请使用类。因此,我删除了id="f_name"并将其添加到类选择器class="the previous classes f_name"

2:要设置输入的值,必须使用.val()而不是.text()

代码语言:javascript
复制
$(clone).find(".f_name").val("Tisha");

Demo

代码语言:javascript
复制
$("#btn_addsector").click(function() {
  var div = document.getElementById("datarow"),
    clone = div.cloneNode(true);

  //neither of the lines work
  $(clone).find(".f_name").val("Tisha");
  $("#maincontent").append(clone);
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="maincontent">
  <button id="btn_addsector" class="bg-transparent hover:bg-secondary-dark text-secondary-dark font-semibold hover:text-white py-2 px-4 border border-secondary-dark hover:border-transparent rounded">
        Add Sector
    </button>

  <div class="px-4 py-6 mb-4 border rounded-lg border-gray-400" id="datarow">

    <div class="flex flex-row space-x-4 pb-5">
      <div class="relative z-0 w-full mb-5">
        <input type="text" name="f_name" placeholder="Enter Name here" value="Hannah" required class="pt-3 pb-2 block w-full px-4 mt-0 rounded bg-white border-0 border-b appearance-none f_name" />
        <label for="f_name" class="absolute duration-300 pl-2 text-lg top-3 z-1 origin-0 text-gray-500">Name</label>
      </div>

      <div class="flex z-0 w-full justify-end">
        <input type="text" name="f_dest" placeholder="Enter Destination here" value="Smallville" required class="pt-3 pb-2 block w-full px-4 mt-0 rounded bg-white border-0 border-b appearance-none f_dest" />
        <label for="f_dest" class="absolute duration-300 pl-2 text-lg top-3 z-1 origin-0 text-gray-500">Destination</label>
      </div>
    </div>
  </div>

</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71081032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档