2013-03-27 74 views
1

这与我以前的问题here有关,但我认为它更简单。在摘要页面上显示复选框选项

我创建了一个一步一步的过程,有几个步骤,最后一步是一个总结页面。每个步骤都包含一些复选框,在摘要页面上我想显示选择的内容,然后允许他们提交表单。

我很努力地理解相关问题的过程,希望这个简化版本能帮助我理解。

这里是我的设置:

<?php $counter = 1; 
$amount = get_field('select_number_of_questions'); 
$repeater = get_field("step_by_step_test"); 
shuffle($repeater); 
$repeater_limit = array_slice($repeater,0,$amount); 
foreach($repeater_limit as $repeater_row) { ?> 
<div class="form-row"> 
     <div id="modules-repeat">       
      <p class="training"><?php echo $repeater_row['question']; ?></p><br /> 
      <p class="training-highlight">Please choose <span class="bold"><?php echo $repeater_row['required_answers']; ?></span> of the following answers:</p><br /> 
      <?php $rows = $repeater_row['answer_options']; 
      foreach ($rows as $row){ ?> 
      <?php $Question[$counter] = $_POST['answer'.$counter]; ?> 
        <div style="display:table-row;"> 
        <div style="display:table-cell;"> 
         <input style="width: 20px;" type="checkbox" name="answer<?php echo $counter; ?>[]" value="<?php echo the_sub_field('answer'); ?>" /> 
        </div> 
        <div style="display:table-cell;"> 
         <p> 
          <?php echo $row['answer']; ?> 
         </p> 
        </div>    
        </div> 
      <?php } ?> 
      <div class="inner"></div> 
      <button class="next"></button> 

      <div class="clear"></div> 
     </div> 
</div> 
<?php $counter++; } ?> 

<div class="form-row" id="form-row-last" style="display:none;"> 
    <div id="modules-repeat">       
     <p>Summary page</p> 
      <?php foreach($repeater_limit as $repeater_row) { ?> 
       <p class="training"><?php echo $repeater_row['question']; ?></p><br /> 
      <?php } ?> 
      <div class="inner"></div> 
      <button class="next"></button> 
      <div class="clear"></div> 
     </div> 
</div> 

这里是jQuery的:

<script type="text/javascript"> 
jQuery(function($) { 
$(document).ready(function() {  

    // prepend a 'previous' button to all form-rows except the first 
    $('<button>').addClass('previous').appendTo($('.inner').not(':first')); 

    // hide all form-rows, but not the first one 
    $('.form-row').not(':first').hide(); 

    // hide on last step 
    $('button.next').last().hide(); 

    // add the submit button to the last form-row 
    $('<input>').addClass('check').prop('type', 'submit').appendTo($('.form-row:last')); 


    // handle the previous button, we need to use 'on' here as the 
    // previous buttons don't exist in the dom at page load 
    $('.form-row').on('click', 'button.previous', function(e) { 
     e.preventDefault(); 
     $(this).parents('div.form-row').hide().prev('div.form-row').show(); 
    }); 

    $('button.next').click(function(e) { 
     // prevent the next buttons from submitting the form 
     e.preventDefault(); 
     // hide this form-row, and show the next one 
     $(this).parents('div.form-row').hide().next('div.form-row').show(); 
    });  

}); 
}); 
</script> 

@Niels目前最接近的答案。这是我的html/php设置,使用@Niels答案中的jquery,它抓住了问题,但不是被检查的答案。

<p class="training"><?php echo $repeater_row['question']; ?></p><br /> 
<p class="training-highlight">Please choose <span class="bold"><?php echo $repeater_row['required_answers']; ?></span> of the following answers:</p><br /> 

<div class="question" data-max-answers="<?php echo $repeater_row['required_answers']; ?>"> 
    <?php $rows = $repeater_row['answer_options']; 
    foreach ($rows as $row){ ?> 
    <?php $Question[$counter] = $_POST['answer'.$counter]; ?> 
    <div style="display:table-row;"> 
     <div style="display:table-cell;"> 
      <input style="width: 20px;" type="checkbox" name="answer<?php echo $counter; ?>[]" value="<?php echo the_sub_field('answer'); ?>" /> 
     </div> 
     <div style="display:table-cell;"> 
      <p> 
       <?php echo $row['answer']; ?> 
      </p> 
     </div>    
    </div> 
    <?php } ?> 
</div> 
<div class="inner"></div> 
<button class="next"></button> 
<div class="clear"></div> 
</div> 
+0

有什么问题吗?你有什么尝试? – Misch 2013-04-01 18:37:58

+0

同样在这里:问题是什么?只需将所有数据存储到SESSION中,并在所有步骤的末尾(最后一步)检查是否所有内容都已提交。成功证明,显示一个总结。 – djot 2013-04-07 22:30:30

回答

1

为什么你不能更改复选框,然后生成一个列表中的LI或中断的选择发布?喜欢的东西:

$("input[type=checkbox]").on("change", function(){ 
    var html = ""; 
    // Get checked checkboxes 
    $(".form-row").each(function(){ 
     var question = $(this).find(".training").text(), 
      checkedlist = []; 
     $(this).find("input:checked").each(function(){ 
      checkedlist.push($(this).val()); 
     }); 
     // Add question and answers to the summary, only if minimum of 1 checkbox checked 
     if(checkedlist.length) 
     { 
      html += "Question: '" + question + "': " + checkedlist.join(", ") + "<br />"; 
     } 
    }); 

    $(".inner").html(html); 
}); 
+0

我回到了这个项目。这非常非常接近!每次我检查一个盒子时,它都会抓住问题并将其输入到.inner div中,但不会抓住他们检查的答案。我用代码更新了我的问题(底部代码框)。就像我说的那样非常接近工作。 – Rob 2013-04-11 09:00:57

+0

我还补充了赏金,因为我错过了实际奖励它。 – Rob 2013-04-11 11:52:23

+1

它应该得到检查的输入。你可以在行中提醒它吗?看看它是否得到了检查值。并警告'$(this).val()',看它是否真的有一个值。 – Niels 2013-04-11 12:46:31

0

你可能会想要这样的东西。不要让它太硬:)

JSfiddle

JSfiddle (with an example for a question)

HTML

<div class="page1">1<br /><button>Next</button></div> 
<div class="page2">2<br /><button>Next</button></div> 
<div class="page3">3<br /><button>Next</button></div> 
<div class="page4">4<br /><button>Next</button></div> 
<div class="page5">5<br /><button>Next</button></div> 
<div class="page6">6 (end)</div> 

CSS

div[class^="page"] { 
    display: none; 
    margin: 20px auto; 
    padding: 15px; 
    width: 400px; 
    height: 200px; 
    box-sizing: border-box; 
    background-color: orange; 
    -moz-box-shadow: 3px 3px 5px 6px #ccc; 
    -webkit-box-shadow: 3px 3px 5px 6px #ccc; 
    box-shadow:   3px 3px 5px 6px #ccc; 
} 

的JavaScript

$(document).ready(function() { 
    $('.page1').css('display', 'block'); 

    $('div[class^="page"] button').click(function(evt) { 
     evt.preventDefault(); 

     // Figure out the current page 
     var page_nr = $(this).parent().attr('class'); 
     page_nr = page_nr.replace('page', '') * 1; 

     // Hide current page 
     $(this).parent().animate(
      { opacity: 'hide', display: 'none' }, 
      750, function() { 
       // Show next page 
       $('.page' + (page_nr + 1)).animate({ opacity: 'show', display: 'block' }, 750); 
      }); 
    }); 
}); 

如果您还希望了解如何处理在每个页面上的表单变量的维护,只需留下一些细节的评论,我会更新的答案建议。

点击转到下一页时,您可以轻松处理每页数据。如果某些数据无效,甚至可以阻止显示下一页。

祝你好运!

3

什么是业务逻辑,呵呵?

*抓斗的页面上所需的输入字段的所有值(用户之前提交的形式),例如:

var name = $('#name').val(); 
... 
var city = $('#citySelectBox').val(); 

*使用根据发明内容部分这些瓦尔用于显示目的。

*审查完这些后,用户将提交表格或修改已填充的字段。

0

也许最简单/容易/无争议的解决办法是展示后“接受”最后的表单页面,而是通过透明覆盖禁用行(不要禁用您输入的所有页面因为这会使提交忽略禁用的字段)。

现在我不能帮你代码,但理论上这是你所需要的...