如何在不重新加载的情况下关闭同一页面中的模式后如何保持用户输入?

时间:2016-06-27 23:45:30

标签: javascript jquery html twitter-bootstrap

我希望能够在模式的复选框中保留用户决定的数据,并使用模式的复选框决定发送它而不离开页面或重新加载。

如果用户决定选中模式中的复选框,当按继续时,如何保留该数据,然后等待提交按钮发送所有复选框的值。

var btn = document.getElementById("myBtn");

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}
/* Modal Content */

.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  border: 1px solid #888;
  width: 80%;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  -webkit-animation-name: animatetop;
  -webkit-animation-duration: 0.4s;
  animation-name: animatetop;
  animation-duration: 0.4s
}
/* Add Animation */

@-webkit-keyframes animatetop {
  from {
    top: -300px;
    opacity: 0
  }
  to {
    top: 0;
    opacity: 1
  }
}
@keyframes animatetop {
  from {
    top: -300px;
    opacity: 0
  }
  to {
    top: 0;
    opacity: 1
  }
}
/* The Close Button */

.close {
  color: white;
  float: right;
  font-size: 28px;
  font-weight: bold;
}
.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
.modal-header {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}
.modal-body {
  padding: 2px 16px;
}
.modal-footer {
  padding: 2px 16px;
  background-color: #5cb85c;
  color: white;
}
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">×</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
      <input type="checkbox" name="option" />
      <input type="submit" value="continue" />
    </div>
  </div>


</div>
<div>
  <input type="checkbox" name="value" />
  <input type="submit" value="Send" />
</div>

2 个答案:

答案 0 :(得分:3)

不要将模态视为与您的页面分开的实体。模态在页面上只是一个普通的div ,但它位于其他内容的中心位置。但它只是一个普通的div,你可以随时读取模态中元素的值,即使它是不可见的。

模态复选框中的值仍然可用,就像它们位于不同的div中一样。如果您的表单在模态中,那么它们仍然是表单的一部分。如果表单在模态之外,那么您希望随时使用javascript / jQuery 来读取该复选框并影响您的表单。

下面是一个示例,其中表单位于模态之外,当模式关闭时,javascript会更新表单字段的值。

&#13;
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div id="approachWrapper" class="col-xs-12 col-md-10 col-md-offset-1">
      <div class="col-xs-12 col-md-3 p-b-2 approachBox">
        <h2 class="text-xs-center p-t-2">test</h2>
        <div class="approachIcon col-xs-12"><i class="fa fa-user fa-3x col-xs-12 text-xs-center"></i>
        </div>
        <p class="text-xs-left">test</p>
      </div>
      <div class="col-xs-12 col-md-3 p-b-2 approachBox">
        <h2 class="text-xs-center p-t-2">test</h2>
        <div class="approachIcon col-xs-12"><i class="fa fa-user fa-3x col-xs-12 text-xs-center"></i>
        </div>
        <p class="text-xs-left">test</p>
      </div>
      <div class="col-xs-12 col-md-3 p-b-2 approachBox">
        <h2 class="text-xs-center p-t-2">test</h2>
        <div class="approachIcon col-xs-12"><i class="fa fa-user fa-3x col-xs-12 text-xs-center"></i>
        </div>
        <p class="text-xs-left">test</p>
      </div>
    </div>
  </div>
</div>
&#13;
$('#myModal').modal('hide');

$('button').click(function(){
  $('#myModal').modal('show');
});

$('#myCB').change(function(){
    $('form input[name=inFormCB]').val( $(this).prop('checked') );
});
&#13;
form{width:100%;padding:30px;background:wheat;}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你必须制作该文本的CSS选项:聚焦