根据用户选择显示摘要而不保存在数据库中

时间:2016-11-29 18:19:35

标签: javascript jquery html css

在提交之前,我希望以模式(已创建)显示,总结他们的选择而不保存到数据库。我在网上找到了一些资料,但对我来说不起作用。

所以这是我的HTML:

     <div id="step-11"> //first choice for the user
    <input type="radio" id="theme1" name="cake_theme" value="wedding"   onchange="total()" /> Wedding 
    <input type="radio" id="theme2" name="cake_theme" value="birthday" onchange="total()" />  Birthday
    <input type="radio" id="theme3" name="cake_theme" value="dedication" onchange="total()" /> Dedication
    </div>

     <div id="step-22"> //2nd choice
Other Flavor : <select name="color" id="color_display" onchange="total()" /><option value="red">red</option>
       <option value="pink">pink</option>
       <option value="blue">blue</option> </select> </div>
    </div>

    <a href="#reviewchoices" class="portfolio-link" data-toggle="modal" onclick="showReviewOrder()" value="submit" > Submit </a> //trigger to display the data.

为了澄清onchange = “total()”,这是我放置这些选项的数量,这对我有用(位于单独的文件中)。

我的js(它在html的同一页面)

<script type="text/JavaScript">
    function showReviewOrder(){
        var review = document.getElementById("step-11").value; // I've tried calling the first Div 'step-11' it shows "undefined"
        reviewchoices.innerHTML= review; //reviewchoices is the id of my Modal
    }
</script>

它显示如下: enter image description here

如果我将其更改为var review = document.getElementById("theme1").value;,它会显示婚礼,但其他单选按钮怎么样? 我应该再做一个Div吗?

对于我的模态:(我不会包含所有的模态代码,直接指向这一点)

<div class="portfolio-modal modal fade" id="reviewchoices" tabindex="-1" role="dialog" aria-hidden="true">
<span id = "display_review"> </span> //where I'll display the user's choices
</div>

希望你能帮助我。非常感谢你。

2 个答案:

答案 0 :(得分:1)

将showReviewOrder的代码更改为此

function showReviewOrder(){
  var review = document.getElementById("step-11");
  var selOption=review.querySelector('input[name=cake_theme]:checked').value;
  var reviewchoices=document.getElementById("reviewchoices");
   reviewchoices.innerHTML= selOption; 

}

window.onload = function() {

}

function total() {}

function showReviewOrder() {
  var review = document.getElementById("step-11");
  var selOption = review.querySelector('input[name=cake_theme]:checked').value;
  var reviewchoices = document.getElementById("reviewchoices");

  reviewchoices.innerHTML = selOption;

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="step-11">
  <input type="radio" id="theme1" name="cake_theme" value="wedding" onchange="total()" />Wedding
  <input type="radio" id="theme2" name="cake_theme" value="birthday" onchange="total()" />Birthday
  <input type="radio" id="theme3" name="cake_theme" value="dedication" onchange="total()" />Dedication
</div>
<div id="step-22">
  Other Flavor :
  <select name="color" id="color_display" onchange="total()" />
  <option value="red">red</option>
  <option value="pink">pink</option>
  <option value="blue">blue</option>
  </select>
</div>
</div>

<a href="#reviewchoices" class="portfolio-link" data-toggle="modal" onclick="showReviewOrder()" value="submit"> Submit </a> 
<div id="reviewchoices" tabindex="-1" role="dialog" aria->
  <span id="display_review"> </span> /
</div>

希望有所帮助

答案 1 :(得分:1)

$('#myForm').submit(function() {
  $('#display_review').empty()
   var userdata = $(this).serializeArray();
        jQuery.each( userdata, function( i, field ) {
  $( "#display_review" ).append( field.name + " " + field.value + "<br> " );
});
   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
<input type="radio" name="radioName" value="1" checked /> 1 <br />
<input type="radio" name="radioName" value="2" /> 2 <br />
<input type="radio" name="radioName" value="3" /> 3 <br />
 <select name="selectarray">
   <option value = "1" >1</option>
   <option value = "2" >2</option>
   <option value = "3" >3</option>
   <option value = "4" >4</option>
 </select><br />
 <input type="submit" name="submit" value="submit">
</form>
<span id="display_review"> </span>