打开引导程序模式时,请选择持久性复选框

时间:2019-06-07 12:08:54

标签: jquery bootstrap-modal

打开模式时,我需要保持输入复选框的选择。但我不知道:

我有一个有关用户的复选框列表。当我选择并按下按钮以打开模式时,我需要保留此选择以发送电子邮件,但是模式中的每个复选框都未选中。

HTML:

  <div>
    <h4>LIST USERS</h4>
       @foreach ($agent as $agent)
          <ul class="user-select">
             <li>
                 <h5>{{ $agent->name }}</h5>
                 <input type="checkbox" name="agents[]"/>
             </li>
          </ul>
       @endforeach

      <p>
         <a href="#" class="button register-interest">Register interest</a 
      </p>
   </div>

MODAL:

<div class="register-modal">
    <div class="modal-box">
        <div class="header">
            <h3 class="text-black">Register your interest</h3>
            <a href="#" title="Close" class="close-modal">X</a>
        </div>
        <div class="content">
            <h5>Select Agents</h5>
                <form>
                  @foreach ($agents as $agent)
                    <ul class="agents-select">
                        <li>
                            <h5>{{ $user->name }}</h5>
                            <input type="checkbox" name="agents[]"/>
                        </li>
                    </ul>
                  @endforeach
                  <input type="text" name="name"/>
                  <input type="email" name="email"/>
                  <textarea name="comments"></textarea>
                  <button type="submit" name="register">SEND</button>
                </form>
          </div>
      </div>
  </div>

JQUERY:

    $('.register-interest').on('click', function(e) {
        e.preventDefault();
        $('.register-modal').show();
    });

    $('.register-modal a.close-modal').on('click', function(e) {
        e.preventDefault();
        $('.register-modal').hide();
    });

真实代码:

HTML:

enter image description here

MODAL:

enter image description here

JQUERY:

enter image description here

打开模式时如何保存输入选择并保持?

最好的问候!

屏幕快照错误

enter image description here

1 个答案:

答案 0 :(得分:1)

我建议您循环浏览第一个列表,然后将其项目的状态复制到第二个列表。

使用jQuery#eq查找相应的循环<input>,然后使用jQuery#prop查找是否被选中:

$('.register-interest').on('click', function(e) {
    e.preventDefault();

    $('.user-select input').each((i, input) => {
        $('.agents-select input').eq(i).prop('checked', $(input).prop('checked'));
    });

    $('.register-modal').show();
});

工作示例:

$('#show-modal').on('click', () => {
  $('#checklist input').each((i, input) => {
    $('#modal input').eq(i).prop('checked', $(input).prop('checked'));
  });

  $('#modal').show();
});

$('#hide-modal').on('click', () => {
  $('#modal').hide();
});
#modal {
  display: none;
  
  border: 1px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<ul id="checklist">
  <li>
    <span>User 1</span>
    <input type="checkbox" name="user[]">
  </li>
  
  <li>
    <span>User 2</span>
    <input type="checkbox" name="user[]">
  </li>
  
  <li>
    <span>User 3</span>
    <input type="checkbox" name="user[]">
  </li>
</ul>

<button id="show-modal">Show modal</button>

<ul id="modal">
  <li>
    <span>User 1</span>
    <input type="checkbox" name="user[]">
  </li>
  
  <li>
    <span>User 2</span>
    <input type="checkbox" name="user[]">
  </li>
  
  <li>
    <span>User 3</span>
    <input type="checkbox" name="user[]">
  </li>
  
  <li>
    <button id="hide-modal">Hide modal</button>
  </li>
</ul>