jQuery + - 按钮不会像我期望的那样输入值

问题描述:

我有一个ajax模块,可以在客户更改数量或所需的所选属性时更新网站上的价格。只要在输入框中输入值,价格就会更新。 我决定添加一些不错的jQuery按钮来增加值,而不是用户点击框然后输入数量。 问题是我用来创建按钮的脚本不会触发价格变化,即使它确实改变了输入字段中的值。jQuery + - 按钮不会像我期望的那样输入值

我的脚本是:

<script language="javascript"> 
jQuery.noConflict(); 
jQuery(document).ready(function() { 
jQuery("#cartAdd").prepend('<div class="dec button">-</div>'); 
jQuery("#cartAdd").append('<div class="inc button">+</div>'); 
jQuery(".button").click(function() { 
var $button = jQuery(this); 
var oldValue = $button.parent().find("input").val(); 

if ($button.text() == "+") { 
var newVal = parseFloat(oldValue) + 1; 
} else { 
if (oldValue >= 1) { 
var newVal = parseFloat(oldValue) - 1; 
} 
} 
$button.parent().find("input").val(newVal); 
}); 
}); 
</script> 

有没有什么办法,使价格更新识别输入字段的新值,我可以让这个功能呢?

+0

点击将只加载DOM元素工作....因为你是后来添加明确...按钮所以使用“on”或“live”... live已被弃用 所以如果您使用的是最新的jQuery lib,请使用它。 – 2013-03-01 13:17:31

+0

如果你正在动态创建按钮,然后使用jQuery的'ON'或'bind'函数。 – run 2013-03-01 13:19:31

+1

你的html看起来像什么? – Tetaxa 2013-03-01 13:19:45

您可能需要使用事件委托,所以你不必担心“活”,如果你正在寻找一个改变事件发生,你必须像Tetaxa说的那样手动完成。就像这样:

<script language="javascript"> 
    jQuery.noConflict(); 
    jQuery(document).ready(function() { 
    var $cartAdd = jQuery('#cartAdd') 
     , $quantity = $cartAdd.find('input') 
     , $price = jQuery('#price'); 

    $cartAdd.prepend('<div class="dec button">-</div>'); 
    $cartAdd.append('<div class="inc button">+</div>'); 

    $cartAdd.click(function(evt) { 
     var $incrementor = jQuery(evt.target) 
     , quantity = parseInt($quantity.val(), 10); 

     if($incrementor.hasClass('inc')) { 
     quantity += 1; 
     } else if($incrementor.hasClass('dec')) { 
     quantity += -1; 
     } 

     if(quantity > 0) { 
     $quantity.val(quantity).change(); 
     } 
    }); 

    $quantity.change(updatePrice); 

    function updatePrice() { 
     var price = 15.50 
     , quantity = parseInt($quantity.val(), 10); 
     $price.text(quantity * price); 
    } 
    }); 

</script> 

这是一个jsFiddle与工作代码。

编辑:我添加了一些代码来处理数量输入上的更改事件。

编辑2:我明白了现在的问题。查看源代码后,看起来您只是在文本输入上查找onkeyup,因此触发更改不会产生任何影响。为您解决眼前的问题,我会代替上述:

 if(quantity > 0) { 
     $quantity.val(quantity).change(); 
     } 

,而是把

 if(quantity > 0) { 
     $quantity.val(quantity); 
     xhr.getPrice(); 
     } 
+0

我试过了,值不会从按钮增加。 – 2013-03-01 14:15:21

+0

Blah。而不是evt.trigger它应该是evt.target。对于那个很抱歉。 – bmavity 2013-03-01 14:32:00

+0

不好意思让你失望,但价值依然不会增加。现在,我把那堵墙撞到了什么地方? – 2013-03-01 14:38:18

您需要使用委托函数,该函数获取页面加载

jQuery(document).on('click','.button',function(){ 
//Code here 
}); 

的文档.on()

try this 1... 

    jQuery(".button").on('click',(function() { 
    var $button = jQuery(this); 
    var oldValue = $button.parent().find("input").val(); 

    if ($button.text() == "+") { 
    var newVal = parseFloat(oldValue) + 1; 
    } else { 
    if (oldValue >= 1) { 
    var newVal = parseFloat(oldValue) - 1; 
    } 
    } 
    $button.parent().find("input").val(newVal); 
    }); 
+0

使用.on的选项都不能解决问题。现在有点挠头。 – 2013-03-01 14:16:45

+0

你正在得到什么错误....? – 2013-03-03 11:03:43

如果我理解正确后加载的元素,在输入字段中的值更改,但你有一些事件处理程序附加到未触发的更改事件,这是正确的吗? 如果是这样,你的代码更改为这个手动触发事件:

$button.parent().find("input").val(newVal).change(); 
+0

试过。换了,没有运气。我已经发布了一个链接,可以在前面的评论中查看源代码。 – 2013-03-01 14:17:58