如何使用弹出窗口在复选框中回显变量值?

时间:2016-05-18 08:08:02

标签: javascript php jquery

我是PHP新手。我有一个带按钮的代码。单击按钮时,将显示一个模态。在模态上,有多个复选框。选中其中一个复选框时,应打印该复选框的值。但是我想在模态的复选框中打印这些值。

Please Check This Fiddle

这是我的代码

$(document)
  .ready(function() {
    $(".various").fancybox({
      maxWidth: 800,
      maxHeight: 600,
      fitToView: false,
      width: '70%',
      height: '70%',
      autoSize: false,
      closeClick: false,
      openEffect: 'none',
      closeEffect: 'none'
    });

    $('.button1').click(function(e) {
      e.preventDefault();
      $('.various').click();
    });

  })
  .on('change', '[name=test-link]', function() {
    $('#print-values').empty();
    $('#print-values').append("<span>You've checked:</span><br />");
    $('[name=test-link]').each(function() {
      if ($(this).prop("checked")) {
        var v = $(this).val();
        $('#print-values').append(v + "<br />");
      }
    });
  });




<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.pack.js"></script>

<a class="various" href="#form1" style='display: none;'></a>
<input class='button1' type="button" value="Link More Opinion" />
<div id='print-values'></div>
<div style='display: none;'>
  <form id='form1'>
    <input type='checkbox' name='test-link' value="1" />
    <span>1</span>
    <br />
    <input type='checkbox' name='test-link' value="2" />
    <span>2</span>
    <br />
    <input type='checkbox' name='test-link' value="3" />
    <span>3</span>
    <br />
  </form>
</div>

2 个答案:

答案 0 :(得分:1)

追加输入并使用输入值

中的值

$('#print-values').append("<input type=text value='"+v+"'><br />");

DEMO

答案 1 :(得分:0)

Fiddle link,请查看此信息并告诉我们。

<div id='print-values'></div>更改为<input type="text" id='print-values'/>

jquery:

只有小提琴中的部分,细节。

$('#print-values').val("");
$('#print-values').append("<span>You've checked:</span><br />");
$('[name=test-link]').each(function() {
    if ($(this).prop("checked")) {
       v = $(this).val();
       $('#print-values').val($('#print-values').val() +" "+ v);
    }
});