在Jquery中为文本和日历控件生成ID

时间:2017-12-07 12:00:54

标签: javascript jquery html css

每当我点击Id's课程时,我想为输入类型text生成.add。目前每当我点击.add类时,它会为每个输入控件生成同名的id。因此,在添加数据时,它无法正常工作。

因此,每当我点击.add课程时,我应该如何为每个文本生成ID。请建议。

这是生成的内容。

Generated HTML

$('.add').on('click', function() {
  var $tr = $(this).closest('tr');
  var $tr2 = $tr.clone(true, true);
  $tr2.find(".vendorName").children('label').remove();
  $tr2.find(".vendorFromDate").children('label').remove();
  $tr2.find(".vendorToDate").children('label').remove();
  $tr2.find(".vendorName").children().unwrap();
  $tr2.find(".vendorFromDate").children().unwrap();
  $tr2.find(".vendorToDate").children().unwrap();
  $tr2.insertAfter($tr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td>
    <div class="row noPadding vendorForm">
      <div class="vendorDaterow">
        <div class="vendorName" id="dvVendorNameData">
          <label>SP Vender Name</label><span><input type="text" name="nmVendorData" id="txtVendorName1" /></span>
        </div>
        <div class="vendorFromDate">
          <label>From Date</label><span class="datepicker"><input type="text" name="nmVendorData" id="spFromDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
        </div>
        <div class="vendorToDate">
          <label>To Date</label><span class="datepicker"><input type="text" name="nmVendorData" id="spToDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
        </div>
      </div>
      <div class="add">
        <i class="fa fa-plus" aria-hidden="true"></i>
      </div>
      <i class="max">(Maximum 5 Vendors)</i>

    </div>
  </td>
</tr>

请建议我应该如何生成动态ID。

2 个答案:

答案 0 :(得分:1)

检查

&#13;
&#13;
(function () {
  var toAddCloneCount = 2;

$('.add').on('click', function() {
  var $tr = $(this).closest('tr');
  var $tr2 = $tr.clone(true, true);
  $tr2.find(".vendorName").children('label').remove();
  $tr2.find(".add").children().remove();
  $tr2.find(".vendorFromDate").children('label').remove();
  $tr2.find(".vendorToDate").children('label').remove();
  $tr2.find('#txtVendorName').prop('id', 'txtVendorName' + toAddCloneCount);
  $tr2.find('#spFromDate1').prop('id', 'spFromDate' + toAddCloneCount);
  $tr2.find('#spToDate1').prop('id', 'spToDate' + toAddCloneCount++);
  $tr2.insertAfter($tr);
});
})();
&#13;
.vendorName,.vendorFromDate,.vendorToDate{
width:33%;float:left;}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
                            <td>
                                <div class="row noPadding vendorForm">
                                    <div class="vendorDaterow">
                                        <div class="vendorName">
                                            <label>SP Vender Name</label><span>@*Shri Kamalkanth Co.*@<input type="text" name="nmVendorData" id="txtVendorName" /></span> 
                                        </div>
                                        <div class="vendorFromDate">
                                            <label>From Date</label><span class="datepicker"><input type="text" id="spFromDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
                                        </div>
                                        <div class="vendorToDate">
                                            <label>To Date</label><span class="datepicker"><input type="text" id="spToDate1" /><i class="fa fa-calendar" aria-hidden="true"></i></span>
                                        </div>
                                    </div>
                                    <div class="add">
                                        <i class="fa fa-plus" aria-hidden="true"></i>
                                    </div>
                                    <i class="max">(Maximum 5 Vendors)</i>

                                </div>
                            </td>
                        </tr>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

嗯,您始终可以根据您拥有的项目生成ID。

你会有像

这样的东西
            $(".add").on("click", function(){
            var numBlocks = $('.vendorDaterow').length; 
/*gets number of elements with this class in DOM*/

            var formStructure = "";
            formStructure = "<div id='formRow"+numBlocks+"' class='vendorDaterow'>";
            formStructure = "<div class='vendorName' id='dvVendorNameData"+numBlocks+"'>";
            formStructure = "<label>SP Vender Name</label><span><input type='text' name='nmVendorData' id='txtVendorName"+numBlocks+"' />";
            /*continue for whole structure*/

            $("#formRow"+(numBlocks-1)).parent().append(formStructure);
     /*finds last existing #formRow, gets his parent node and append your new node into it. Also you can do your other logic based on id, like hiding the add button or something.*/


        });

请注意,计数ID从0开始(你什么都没有,长度为0.所以第一个添加的节点将是#formRow0。