【问题标题】:jquery .animate() called on click anywhere on pagejquery .animate() 点击页面上的任意位置调用
【发布时间】:2017-09-18 09:42:20
【问题描述】:

我有下面的 jquery。一切正常,除了....如何在提交按钮之外获得点击以停止触发动画?

这是否与先点击输入框,然后添加值,然后点击是第二次变化有关?

<form action="" method="post">
    <div id="<?php echo $this->item['id']; ?>">
        <div class="ab">
            <label for="ab_input"><?php echo $this->translate('To get the auction started, you must enter a starting bid.'); ?></label>
        <span class="auction_bid_container">
            <input id="ab_input" type="text" maxlength="12"
                   class="middle nmr event-clear-focus"
                   name="bidz" value="Enter something" />
            <input id="updateBidButton" type="submit"
                   class="button grey-button num-items-button"
                   name="bidz" 
                   value="<?php echo $this->translate('submit'); ?>"/>
        </span>
        </div>
    </div>
</form>
<div class="clear mb20"></div>



$('input[name="bidz"]').live('change', function () {
    var bidz = this.value;
    $.ajax({
        type: 'post',
        url: "?module=is&controller=index&action=syy",
        dataType: "text",
        data: 'bid=' + bidz + '&id=' + id,
        beforeSend: function () {
            $('.ab').animate({
                'backgroundColor': '#ffdead'
            }, 400);
        },
        success: function (result) {
            if (result == 'ok') {
                console.log(bidz);

                $('.ab').animate({
                    'backgroundColor': '#A3D1A3'
                }, 300);
            } else {
                $('.ab').animate({
                    'backgroundColor': '#FFBFBF'
                }, 300);
            }
        }
    });
});

【问题讨论】:

  • 你的html代码中input[name="bid_amount"]在哪里,根据你的htmk你有input[name="bidz"]
  • 抱歉@SuperUser,我已将bid_amount 更改为bidz。
  • @ian 你的jQuery 是什么版本?
  • @DavidR 这是一个旧版本 - 1.8.1。我需要更新,但没有意识到 b/c 有太多遗留代码。

标签: javascript jquery html jquery-animate inputbox


【解决方案1】:

如果您只想在提交按钮之外单击以停止触发动画,那么您应该将事件处理程序附加到提交按钮而不是文本输入。这样的事情应该可以正常工作:

$('#updateBidButton').live('click', function (e) {
e.preventDefault();
var bidz = this.value;
$.ajax({
    type: 'post',
    url: "?module=is&controller=index&action=syy",
    dataType: "text",
    data: 'bid=' + bidz + '&id=' + id,
    beforeSend: function () {
        $('.ab').animate({
            'backgroundColor': '#ffdead'
        }, 400);
    },
    success: function (result) {
        if (result == 'ok') {
            console.log(bidz);

            $('.ab').animate({
                'backgroundColor': '#A3D1A3'
            }, 300);
        } else {
            $('.ab').animate({
                'backgroundColor': '#FFBFBF'
            }, 300);
        }
    }
});
});

编辑:

另外请记住,live() 方法在 jQuery 1.9 中被删除了,所以请考虑将其替换为:

$(document).on('event', 'yourSelector', function() {
    // your code hear
});

所以你的情况是:

$(document).on('click', '#updateBidButton', function() {
    // ...rest of your code...
});

这会将一个事件附加到文档对象,然后当您单击一个元素时,它会检查目标是否为 #updateBidButton,因此即使 html 是动态生成的,它也能正常工作。 p>

【讨论】:

  • 发送@Fuross。我离开“#updateBidButton”选择器的原因是该功能在 safari 和 firefox 上不起作用。当我使用 'input[name="bid_amount"]' 选择器时,正如我在其他 jquery 函数上成功完成的那样,它适用于所有 4 个主要浏览器。关于那里发生的事情或如何解决的任何想法?
  • @ian 如果没有看到实际代码,我无法告诉您太多信息,由于在新的 Web 浏览器中使用真正旧版本的 jQuery,可能存在一些兼容性问题,但这不太可能,也您在按钮上使用了事件吗?改变?点击?如果你能给我一个包含所有代码的 JSFiddle,我可以告诉你更多。
  • @ian 另外,我假设我编辑并在我的答案中提供的代码在所有浏览器中都适用于您?
  • @Faross,仍在尝试让您的代码正常工作。以前在 chrome/ie 但不是 safari/firefox 的工作是这样的:[Jsfiddle] (jsfiddle.net/kanyamyb/2)
  • 如果有帮助的话,我可以给你一个现场测试用例。
猜你喜欢
  • 1970-01-01
  • 2013-08-12
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-31
相关资源
最近更新 更多