-2

編集:上記の提案は、この質問に対する答えではありません。timeJVの回答をご覧ください。

フォームにオファー コード フィールドがありますが、適用されたら停止したいと思います。現在、オファーコードを入力し続けると、価格が割引され続けます。

JS フィドルのデモ

どうすればこれを止めることができますか?

$("#offerapply").click(function () {
if ($("input[name='offercode']").val().toLowerCase() == "discount10")  {
    price = (price / 10) * 9;
    $("#offermsg").text('Thank you. Your 10% discount has been applied.');
}
else {
    $("#offermsg").text('Sorry, that Offer Code was not recognised.');
}
calculate();
4

4 に答える 4

6

真のブロック内ifで、複数の割引を防ぐためにバインドを解除します。

if ($("input[name='offercode']").val().toLowerCase() == "discount10")  {
    price = (price / 10) * 9;
    $("#offermsg").text('Thank you. Your 10% discount has been applied.');
    $(this).off();
}
于 2013-07-18T20:53:51.453 に答える
3

one jquery メソッドを見てください。したがって、このコードを試すことができます:

$("#offerapply").one('click', function () {
    // your code here
})
于 2013-07-18T20:53:36.733 に答える
1

デモ - アプローチ #1

$('a.remove_item').on('click',function(e) {
    alert('clicked');
   $('a.remove_item').off('click');
});

デモ - アプローチ #2

$(document).ready(function(){
    $("#offerapply").one('click', function () {
        // your code here
    })
});

デモ - アプローチ #3

$.fn.liveAndLetDie = function(event, callback) {
    var sel = this.selector;
    function unbind() { $(sel).die(event, callback).die(event, unbind); }
    return this.live(event, callback).live(event, unbind);
};

</p>

$('your elements').liveAndLetDie('click', function(e) { /* do stuff */ });

デモ - アプローチ #4

$('your element').live('click',function(e) {
     $('your element').die('click'); // This removes the .live() functionality
});
于 2013-07-18T20:54:51.837 に答える