按钮的单击事件没有不同的行为

时间:2015-11-30 20:45:42

标签: javascript jquery html

我有一些PHP和HTML:

<div class="bought">
    <div class="row">
        <div class="col">
            <div class="body">
                <?php if(Request::is('user/*')) { ?>
                <div>Did you buy this for <?php echo $user->username ?>?</div>
                <div class="options">
                    <a class="boughtyes cbutton whiteonpurple">Yes</a>
                    <a class="boughtno cbutton whiteonpurple">No</a>
                </div>
                <?php } else { ?>
                <div>Bought?</div>
                <p>Click here to send hinters a message to let them know.<br />And yes, it can still be a surprise!</p>
                <?php } ?>
            </div>
        </div>
    </div>
</div>

一些jQuery应该根据选择简单地打开一个没有或是的JavaScript窗口:

$("#view-hint .img .bought .boughtyes").click(function() {
    window.alert("yes");
});
$("#view-hint .img .bought .boughtno").click(function() {
    window.alert("no");
});

但是,无论我点击是或否按钮,它都会返回'是':

enter image description here

我做错了什么?

编辑:

使用chrome看起来元素是分开的: enter image description here

EDIT2: enter image description here

1 个答案:

答案 0 :(得分:2)

a按钮都具有相同的class

请纠正它们。这就是.boughtno完全没有应用的原因。看到他们都有class="boughtyes"。看到你的代码,这是正确的,这意味着你需要努力刷新。尝试使用 Ctrl + Shift + R 或清除缓存。

如果您使用的是Laravel或Cake等框架,请尝试重新启动并再次编译资产并尝试。我使用过vagrant。对于这些我通常使用:

vagrant reload --provision

一些信息:Laravel 5 clear views cache