简体   繁体   English

所需的属性不适用于克隆元素

[英]Attributes required not working on cloned elements

I have some elements with the required attribute and I wrote a jQuery code to clone this element.我有一些具有required属性的元素,我编写了一个 jQuery 代码来克隆这个元素。 When I press submit, it should show a message indicating to fill the inputs, but it doesn't work on the cloned elements...当我按下提交时,它应该显示一条消息,指示填充输入,但它不适用于克隆的元素......

<div class='col-md-12'>
  <div class='box box-primary'>
    <div class='box-header with-border'>
      <h3 class='box-title'>Information of Person</h3>
    </div>

    <div class='form-group'>
      <div class='box-body rowClone2'>

        <div class='row rowClone'>
          <div class='col-xs-6 col-md-3'>
            <input type="text" class="form-control" required>
          </div>
          <div class='col-xs-6 col-md-3'>
            <input type="text" class="form-control" required>
          </div>
          <div class='col-xs-6 col-md-3' >
            <input type="tel" class="form-control">
          </div>
          <div class='col-xs-6 col-md-3'>
            <input type="text" class="form-control" required>
          </div>
        </div>
        <br>

      </div>
      <div class="row">
        <div class="col-md-3">
          <input type="submit" value="Submit" formnovalidate>                  
        </div>
      </div>
    </div>

I'm not sure if that is your entire HTML, but you may be missing your form that wraps that block of HTML.我不确定这是否是您的整个 HTML,但您可能缺少包装该 HTML 块的表单。 This code works for me whether the input was there on page load, or cloned using the button I added.无论输入是在页面加载时出现还是使用我添加的按钮进行克隆,此代码都适用于我。

I'm guessing the problem lies with your form tag, whether it's missing or malformed.我猜问题出在你的表单标签上,无论它是丢失还是格式错误。

 $(function() { $(document).on('click', '.cloneLastInput', function() { var clone = $('.form-control:last').closest('div').clone(); $('.rowClone').append(clone); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form action="post"> <button type="button" class="cloneLastInput">clone last input</button> <div class='col-md-12'> <div class='box box-primary'> <div class='box-header with-border'> <h3 class='box-title'>Information of Person</h3> </div> <div class='form-group'> <div class='box-body rowClone2'> <div class='row rowClone'> <div class='col-xs-6 col-md-3'> <input type="text" class="form-control" required="required"> </div> <div class='col-xs-6 col-md-3'> <input type="text" class="form-control" required> </div> <div class='col-xs-6 col-md-3'> <input type="tel" class="form-control"> </div> <div class='col-xs-6 col-md-3'> <input type="text" class="form-control" required> </div> </div> <br> </div> <div class="row"> <div class="col-md-3"> <input type="submit" value="Submit"> </div> </div> </div> </div> </div> </form>

I solve by make loop via jquery add new id or name for each new clone for ex:-我通过 jquery 的 make 循环解决为每个新克隆添加新的 id 或名称,例如:-

 var num =4; var count = $('#c3').val(); for(i = 1 ; count>i ; i++){ var clone = $('.rowClone:first').clone(); $('.rowClone2').append(clone,'<br>') } $('.rowClone').not(':first').find('input').each(function(){ num++ $(this).prop('name','info'+num); $(this).prop('required'); });

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

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