2014-12-13 49 views
0

编辑更新全码:jQuery的事件处理被点击复选框时

我的工作我的第一个JavaScript的情况下,我在这里卡住了:

我有以下几点:

$(document).ready(function() 
 
\t { 
 
\t \t $('input, select').on('focus' ,swapPersonClass); 
 

 
\t \t //Blur = Bind an event handler to the “blur” JavaScript event, or trigger that event on an element. 
 
\t 
 
\t \t $('input, select').on("change", updateCart); 
 
\t } 
 
); 
 

 

 
// Functions 
 
var swapPersonClass = function() 
 
{ 
 
\t var expression = $(this).attr('id'); 
 
\t switch(expression) 
 
\t \t { 
 
\t \t \t case "fullname": 
 
\t \t \t \t break; 
 
\t \t \t case "email": 
 
\t \t \t \t $("#arm").removeClass().addClass("pointemail"); 
 
\t \t \t \t break; 
 
\t \t \t case "countpizzas": 
 
\t \t \t \t $("#arm").removeClass().addClass("pointamount"); 
 
\t \t \t \t break; 
 
\t \t \t default: 
 
\t \t \t \t $("#arm").removeClass().addClass("pointothers"); 
 
\t \t } 
 
}; 
 

 
var updateCart = function() { 
 
\t var change = $(this).attr('id'); 
 
\t console.log(change); 
 
\t 
 
\t $('input, select').each(function() 
 
\t { 
 
\t \t switch (change) 
 
\t \t { 
 
\t \t \t case "fullname": 
 
\t \t \t \t $("#displayname").text($('#fullname').val() + ','); 
 
\t \t \t \t break; 
 
\t \t \t 
 
\t \t \t case "countpizzas": 
 
\t \t \t \t aantalpizzas = $("#countpizzas").val(); 
 
\t \t \t \t if (aantalpizzas > 1) { 
 
\t \t \t \t \t $('.plural').show(); 
 
\t \t \t \t } else { 
 
\t \t \t \t \t $('.plural').hide(); 
 
\t \t \t \t }; 
 
\t \t \t \t $("#displayamount").text(aantalpizzas); 
 
\t \t \t \t console.log(aantalpizzas); 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case "pizzatype": 
 
\t \t \t \t PrijsPizza = $("#pizzatype option:selected").data("price"); 
 
\t \t \t \t console.log(PrijsPizza); 
 
\t \t \t \t break; 
 
\t \t \t \t \t \t 
 
\t \t \t case "YesOption": 
 
\t \t \t \t $("#toppings").show(); 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case "NoOption": 
 
\t \t \t \t $("#toppings").hide(); 
 
\t \t \t \t break; 
 
\t \t \t \t 
 
\t \t \t case "toppings": 
 
\t \t \t \t updateTopping(); 
 
\t \t \t \t break; 
 
\t \t }; 
 
\t \t 
 
\t \t \t $("#currenttotal").text(aantalpizzas * PrijsPizza); 
 
\t }); 
 
}; 
 

 

 
var updateTopping = function() 
 
{ 
 
\t $(".chk_topping").change(function() 
 
\t { 
 
\t \t var selected_topping = $(this).attr('data-value'); 
 
\t \t \t switch(selected_topping) 
 
\t \t \t { 
 
\t \t \t case "salami": 
 
\t \t \t \t $("#currenttotal").text(aantalpizzas * PrijsPizza); 
 
\t \t \t \t PrijsTopping += 0.30; 
 
\t \t \t \t break 
 
\t \t \t } 
 

 
\t }); 
 
}; 
 

 
var PrijsTopping = 0.30; 
 
var aantalpizzas = 0; 
 
var PrijsPizza = 0;
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Forms</title> 
 
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Lobster"> 
 
    <link href='http://fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'> 
 

 
    <link rel="stylesheet" type="text/css" href="assets/css/reset.css"/> 
 
    <link rel="stylesheet" href="assets/css/screen.css" type="text/css"/> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
 

 

 
</head> 
 
<body> 
 
\t <div id="container"> 
 
    <header> 
 
     <h1>Pizza Palace</h1> 
 

 
    </header> 
 
    <aside> 
 
     <h2><span>Order details</span></h2> 
 

 
     <div id="total"> 
 
      <p>Your current order total: &euro; <strong id="currenttotal">0</strong></p> 
 
     </div> 
 

 
     <p><strong id="displayname"></strong> you are ordering <strong id="displayamount"></strong> pizza<span 
 
       class="plural">s</span></p> 
 

 
     <h3>Chosen toppings:</h3> 
 
     
 
     <div id="toppingmessage"> 
 
      <p>No toppings selected</p> 
 
      <ul> 
 
       <li class="hide">Salami</li> 
 
       <li class="hide">Olives</li> 
 
       <li class="hide">Ansjovis</li> 
 
      </ul> 
 
     </div> 
 
     <h3>Your discount: </h3> 
 

 
     <p id="displaydiscount"></p> 
 

 
    </aside> 
 
<form action="#" method="post"> 
 
    <figure id="arm"></figure> 
 
    <figure id="face"></figure> 
 
    <fieldset class="personbody"> 
 

 
    <form> 
 
     <div> 
 
      <label for="fullname">Full Name 
 
      <input id="fullname" name="fullnames" type="text" required autofocus> 
 
      </label> 
 
     </div> 
 
     <div> 
 
      <label for="email">Email 
 
      <input id="email" name="email" type="email" placeholder="Enter Your email" required> 
 
      </label> 
 
     </div> 
 
     <div> 
 
      <label for="howmany">How many pizzas would you like?<br><br> 
 
      <input id="countpizzas" name="countpizzas" type="number" min="1" max="3" step="1" value="0" required> 
 
      </label> 
 
     </div> 
 
     <div> 
 

 
     <label for="pizzatype">Which type of pizza would you like ? <br> 
 
       <select id="pizzatype"name="pizzatype"> 
 
        <option data-price="4" value="S">Small</option> 
 
        <option data-price="5.5" value="M">Medium</option> 
 
        <option data-price="7" value="L">Large</option> 
 
       </select> 
 
\t \t </label> 
 
\t \t </div> 
 
     <div> 
 
      <label for="extratopping">Would you like extra topping? <br> 
 
       <input id="YesOption" name="ExtraTopping" type="radio"> Yes 
 
\t \t \t \t <input id="NoOption" name="ExtraTopping" type="radio"> No 
 
     \t \t </label> \t \t \t 
 
     </div> 
 
     
 
     <div class="hide" id="toppings"> 
 
      <label for="toppings">Which toppings would you like?</label> 
 
       <input type="checkbox" class="chk_topping" data-value="salami"> Salami 
 
       <input type="checkbox" class="chk_topping" data-value="olives"> Olives 
 
       <input type="checkbox" class="chk_topping" data-value="ansjovis"> Ansjovis 
 
     </div> 
 
     <div> 
 
      <label for="deliverydate">When do you want the pizza<span class="plural">s</span> to be delivered? 
 
      \t <input id="delivery" name="delivery" type="date" required > </label> 
 
      <label for="deliverytime"> 
 
      \t <input id="Time" name="Time" type="time" required> </label> 
 
     </div> 
 
      <label for="discountcode">Do you have a discount code? 
 
      <input id="DiscountCode" name="DiscountCode" type="text" pattern="[1-9]{1}-[A-Z]{3}" title="The discount code should be a digit followed by a dash and then 3 uppercase letters" required> 
 
      </label> 
 
     </div> 
 
    </form> 
 
     <input type="submit" value="Place your order" name="placeorder"/> 
 
    </fieldset> 
 
</form> 
 

 
    <footer><p>Graphics courtesy of &copy; Basecamp </p></footer> 
 
</div> 
 

 
\t <script type='text/javascript' src='assets/js/jquery.js'></script> 
 
    <script type='text/javascript' src='assets/js/script.js'></script> 
 
</body> 
 
</html>

为什么我的复选框一直告诉我,这是不确定的,当s选举他们? 我真的不擅长的JavaScript,所以我的代码可能会奔忙

+0

如果'case'部分和'updateTopping()'函数被删除,您的代码似乎正在工作。真的有什么问题? – 2014-12-13 15:05:42

+0

你的代码似乎工作正常。 http://jsfiddle.net/fcs8c362/。您可能需要逐行调试以查看异常情况。唯一的另一件事是元素不会被加载到DOM中,但这取决于您拥有的其他代码。 – haxtbh 2014-12-13 15:10:36

回答

0

试试这个 var selected_topping = $(this).data('value');

http://jsfiddle.net/f4Lghy6p/

+0

没有解决它。 – Awa 2014-12-13 15:08:14

+2

适合我,你把它放在$(document).ready()中吗? – midan888 2014-12-13 15:11:19

+0

$(文件)。就绪(函数() \t { \t \t $( '输入,选择')上( “变化”,updateCart); \t} ); 在我的Updatecart功能,我有我的开关 和在我的情况下我的开关我有一个新的功能UpdateTopping – Awa 2014-12-13 15:17:57

0

发现了它。

我在我的select en输入字段中搜索specefic ID: $('input,select')。on(“change”,updateCart);

我的复选框位于DIV中,输入字段没有ID。