Jquery事件被解雇了两次

时间:2015-10-09 06:56:45

标签: javascript jquery

我正在尝试学习Jquery,我正在做一些实验。这是我的标记:

<div id="wrapper">
    <p id="success"></p>
    <h1>Break the views</h1><br>
    <img id ="product" src="Tshirt2.jpg">
    <div id="our-prices">
        <span>Price: </span><span id ="price">10</span><span> €</span><br>
        <span>Tax: <span id = "tax">2,3</span><span> €</span><br>
        <span>Total: </span><span id = "total">12,3</span><span> €</span><br>
        <input id ="qty" type="range" value="1" min="0" max="10"></input><br>
        <span>Picked: </span><span id ="qty">1</span><span> product(s):</span><br><br>
        <button id="buy">ADD TO CART</button>
    </div>
$(document).ready(function() {
    $("#success").hide();
    var qty = $("#qty").val();
    console.log(qty);

    $("#buy").click(function(){
        $("#success").fadeIn(500);
        $("#success").text(qty + " products were added to your cart");
        $("#success").fadeOut(500);
    });
});

$("#qty").change(function(){
    var qty = $(this).val();
    var price = qty * 10;
    var tax  = price * 0.23;
    var total = price + tax;

    $("#price").text(price);
    $("#tax").text(tax.toFixed(2));
    $("#total").text(total);
    $("span #qty").text(qty);

    $("#buy").click(function(){
        $("#success").fadeIn(500);
        $("#success").text(qty + " products were added to your cart");
        $("#success").fadeOut(500);

如果在#qty中发生更改事件,则成功事件(隐藏/淡出)将触发两次。另一方面,如果我没有在var qty中声明document.ready,则会解决此问题,但在页面加载时未定义var qty的初始状态。为什么会这样?基本上我的问题是如何在.changed事件中可用变量数量的情况下重写这个? 谢谢

3 个答案:

答案 0 :(得分:1)

备注:

  • 你有两个id = qty
  • 的元素
  • 你宣布$(“#buy”)。点击(function()两次
  • 要在整个文档中使用var qty,你必须声明一次,然后你可以在(但不是重新声明)之后更改值

运行此代码段以查看结果

import _ = require('lodash');

答案 1 :(得分:0)

请删除上次购买按钮事件代码。

当qty更改时你有绑定事件所以当你改变数量时它会增加绑定所以从$(&#34; #qty&#34;)中删除买事件.change(function(){function

$("#buy").click(function(){
$("#success").fadeIn(500);
$("#success").text(qty+" products were added to your cart");
$("#success").fadeOut(500);

答案 2 :(得分:0)

我认为在开始使用jQuery之前,你需要先阅读一些关于JavaScript的基础知识和语法,因为这里有几个简单的错误。

当前的问题是,您的Dim wrkSpace As Workspace Dim dbThisDB As DAO.Database Dim rsBilling As DAO.Recordset Dim strBillingSQL As String Set wrkSpace = CreateWorkspace("GVUsers", "admin", "", dbUseJet) Set dbThisDB = wrkSpace.OpenDatabase("GV User Analysis.accdb") strBillingSQL = "SELECT Billing.ID, Billing.CompanyCode, Billing.CompanyName, Billing.TraderName, Billing.TraderID, Billing.ConnectionType," _ & "Billing.IsDisabled, Billing.SetupDate, Billing.TPLogCount, Billing.HasConcession, Billing.IsBillable, Billing.BillingPeriod, Billing.DeskByOrdersTrades," _ & "Billing.DeskByPermissions, Billing.OverallDeskAllocation, Billing.HasGasPermissions, Billing.HasPowerPermissions, Billing.HasCoalPermissions, Billing.TradesCount," _ & "Billing.OrdersCount, Billing.AllocationGas, Billing.AllocationPower, Billing.AllocationCoal, Billing.AllocationMethod,Billing.AsOfDate" _ & " FROM Billing;" Set rsBilling = dbThisDB.OpenRecordset(strBillingSQL, dbOpenDynaset) '<---- 3061 error is returned here 功能永远不会关闭,而且您还要拨打$("#qty").change()两次。作为起点进行以下更改......

$("#buy").click