提问者:小点点

jquery只克隆文本而不克隆不起作用的属性


我正在研究jquery克隆。其中我有一个div和三个元素。第一个元素是select下拉,第二个是text box,第三个是add Button。当用户单击add按钮时,它就完美地克隆了整个div。但是,如果用户从下拉列表的值中选择为“其他”,则需要启用最接近的文本框,这将是第一次运行良好。如果用户单击“添加”按钮,则文本框应处于禁用模式,因为用户尚未从下拉字段中选择另一个。

这是我的jquery代码

null

$(document).ready(function() {
  $("#btn_add").click(function() {
    $("#duplicate").clone(true).insertAfter("#duplicate");
  });
  $("#txt_select").change(function() {
    if ($(this).find("option:selected").val() == "Other") {
      $("#sel_ipt").find("#sel_ipt").removeAttr("disabled").val('');
    } else {
      $("#sel_ipt").attr("disabled", "disabled");
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<div id="duplicate">
  <select id="txt_select">
    <option>Select</option>
    <option value="First">First</option>
    <option value="Second">Second</option>
    <option value="Third">Third</option>
    <option value="Other">Other</option>
  </select>
  <input type="text" disabled id="sel_ipt" />
  <input type="submit" id="btn_add" value="Add" />
</div>

null

请引导我

这是我的JSBIN链接


共2个答案

匿名用户

ID在DOM中应该始终是唯一的。当您现在克隆时,您有几个#sel_ipt(以及其他)是不正确的。如果你要用这些做一个表单发布,你可能应该跟踪你有多少输入,并在副本中添加一个数字。如id=“sel_ipt_2”name=“sel_ipt_2”。

$(document).ready(function(){

 $(".txt_select").on('change',function () {

   // Find the input in the specific row
   var input = $(this).parent().find('.sel_ipt');

   input.attr("disabled", $(this).val() != "Other").val('');

  /* 
  This is (probably) better for readability

  if ($(this).val() == "Other") {
     input.attr("disabled", $(this).val() == "Other").val('');
   } else {
     input.attr("disabled", true).val('');
   }*/

 }); 

 $("#btn_add").click(function(){

   // Get the parent row for cloning
   var row = $(this).parent();

   // Insert the row after the cloned row
   var clonedRow = row.clone(true).insertAfter(row);

   // Disable and remove value in the cloned row
   clonedRow.find('.sel_ipt').attr('disabled', true).val('');

 }); 
});

这里有一个JS-bin链接

匿名用户

首先,由于您正在重复,所以不要使用id,而是使用class

我改变了你的函数,添加了注释,让你明白函数是如何工作的。

null

$( function(){
  $(".btn_add").click(function(){
    // clone the first .duplicate
    var clone = $(".duplicate:first").clone(true);
    // find the text input, change the disabled property and empty the value
    clone.find(".sel_ipt").attr("disabled","disabled").val('');
    // insert after the last .dublicate
    clone.insertAfter(".duplicate:last");
  });
  $(".txt_select").change(function () {
    if ($(this).find("option:selected").val() == "Other") {
      // use next to find the next element with class .sel_ipt
      $(this).next(".sel_ipt").removeAttr("disabled").val('');
    } else {
      $(this).next(".sel_ipt").attr("disabled","disabled");
    }
  }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="duplicate">
  <select class="txt_select">
    <option>Select</option>
    <option value="First">First</option>
    <option value="Second">Second</option>
    <option value="Third">Third</option>
    <option value="Other">Other</option>
  </select>
  <input type="text" disabled class="sel_ipt" />
  <input type="submit" class="btn_add" value="Add" />
</div>