简体   繁体   English

无法将名称属性动态添加到表单元素

[英]Unable to dynamically add name attribute to form element

I wish to dynamically add the name attribute as 'pickup_city2' and 'pickup_address2' to select elements with ids, pickup_cityExtend and pickup_addressExtend. 我希望将name属性动态添加为“ pickup_city2”和“ pickup_address2”,以选择具有ID,pickup_cityExtend和Pick_addressExtend的元素。

 $('#multiCheck').change(function() { if (this.checked) { var $pick = $('#cityPickExtend'); $clone = $pick.clone().removeClass('hide').removeAttr('id').insertAfter($pick); var city = document.getElementById('pickup_cityExtend'); city.setAttribute('name', 'pickup_city2'); var address = document.getElementById('pickup_addressExtend'); address.setAttribute('name', 'pickup_address2'); } if (!this.checked) { $clone.remove(); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="cityPick form-horizontal form-label-right" action="" method="POST" novalidate>{% csrf_token %} <div class="form-group"> <div class="city col-md-4 col-sm-4 col-xs-10"> <div class="item form-group"> <label class="control-label" for="city">City<span class="required">*</span> </label> <div class=""> <select class="form-control" id="city" name="pick_up_city"> <option>Select City</option> <option>Mumbai</option> <option>Delhi</option> <option>Jaipur</option> </select> </div> </div> </div> <div class="address col-md-7 col-sm-7 col-xs-10"> <div class="item form-group"> <label class="control-label" for="address">Address<span class="required">*</span> </label> <div class=""> <input type="text" class="form-control" id="address" name="pick_up_address"> </div> </div> </div> <div class="multiCheck col-md-4 col-sm-4 col-xs-12"> <input type="checkbox" value="Yes" id="multiCheck">Have more than one pickup point? <br> </div> </div> <div class="form-group hide" id="cityPickExtend"> <div class="city col-md-4 col-sm-4 col-xs-10"> <div class="item form-group"> <label class="control-label" for="city">City<span class="required">*</span> </label> <div class=""> <select class="form-control" id="pickup_cityExtend" name=""> <option>Select City</option> <option>Mumbai</option> <option>Delhi</option> <option>Jaipur</option> </select> </div> </div> </div> <div class="address col-md-7 col-sm-7 col-xs-10"> <div class="item form-group"> <label class="control-label" for="address">Address<span class="required">*</span> </label> <div class=""> <input type="text" class="form-control" id="pickup_addressExtend" name=""> </div> </div> </div> <div class="removeBtn col-md-1 col-sm-1 col-xs-2"> <button type="button" id="removeBtn">Remove</button> </div> <div class="addBtn"> <button type="button" id="addBtn">Add another pickup location</button> </div> </div> <div class="item form-group"> <label for="shipment_datetime" class="control-label dateTime">Pickup Date &amp; time <span class="required">*</span> </label> <div class="input-group date form_datetime col-md-4 col-sm-4 col-xs-12" data-date="" data-date-format="dd MM yyyy - HH:ii p" data-link-field="dtp_input1"> <input class="form-control" size="16" name="shipment_datetime" type="text" value="" readonly style="background-color: #fff;"> <span class="input-group-addon"> <span class="glyphicon glyphicon-remove"></span> </span> <span class="input-group-addon"> <span class="glyphicon glyphicon-th"></span> </span> </div> </div> </form> 

Below is my jquery code. 下面是我的jQuery代码。

$('#multiCheck').change(function() {
  if (this.checked) {
    var $pick = $('#cityPickExtend');
    $clone = $pick.clone().removeClass('hide').removeAttr('id').insertAfter($pick);
    var city = document.getElementById('pickup_cityExtend');
    city.setAttribute('name', 'pickup_city2');
    var address = document.getElementById('pickup_addressExtend');
    address.setAttribute('name', 'pickup_address2');
  }
  if (!this.checked) {
    $clone.remove();
  }

})

Within the part that you clone, there are four elements that have an id attribute. 在您克隆的部分中,有四个具有id属性的元素。 As id values must be unique, the DOM API will always return the first match when you query for a certain id , such as in these lines: 由于id值必须是唯一的,因此当您查询某个id时,DOM API将始终返回第一个匹配项,例如以下几行:

var city = document.getElementById('pickup_cityExtend');
var address = document.getElementById('pickup_addressExtend');

The results do not match the elements in the part you added to the document. 结果与您添加到文档中的零件中的元素不匹配。

In order to make it work, you need to replace the id values with something that is unique (by adding a 2 for instance). 为了使其正常工作,您需要用唯一的东西替换id值(例如,添加2 )。

As a side note: you are mixing jQuery syntax with native DOM methods to retrieve elements. 附带说明:您将jQuery语法与本机DOM方法混合使用以检索元素。 It would be more consistent if you would not use document.getElementById , but the jQuery $('#...') equivalent. 如果您不使用document.getElementById ,而是使用jQuery $('#...')等效项,它将更加一致。

Here is some adjusted code: 这是一些调整后的代码:

var $clone;
$('#multiCheck').change(function() {
  if (this.checked) {
    var $pick = $('#cityPickExtend');
    $clone = $pick.clone().removeClass('hide').removeAttr('id');
    // Add '2' to all ID values, and set name value to the same.
    $clone.find('[id]').each(function () {
        var id = $(this).attr('id') + '2';
        $(this).attr('id', id).attr('name', id);
    });
    // Now that the id value are unique, it is OK to add the clone:
    $clone.insertAfter($pick);
  } else if ($clone) { // Check whether we actually have a clone
    $clone.remove();
  }
});

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

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