0

这是我的html

<input type="text" name="post[price]" id="productUpdate" class="test" disabled />
<div class="abs-locator" id="eventDrive"></div>

这是我的CSS

.abs-locator{
  width: 17.8%;
  position: absolute;
  margin-top: -29px;
  margin-left: 150px;
  height: 27px;
}

单击隐藏输入框时我需要执行单击事件

这是我的js

$('#eventDrive').on('click', function() {
    alert('test');
});

但是这里什么也没有发生。为什么会发生这种情况以及如何解决它

4

3 回答 3

2

要在 Ajax 加载的内容上产生点击事件,您必须在容器上注册该事件。我会做这样的事情:

$(document).on('click', '#eventDrive', function() {
    alert('test');
});

这样,点击监听器将在整个文档上注册,并且每次单击它都会检查您是否单击了#eventDrive,即使在您注册监听器时该元素不存在。

于 2013-10-26T19:17:27.627 回答
0

禁用的元素不会触发鼠标事件。大多数浏览器会将源自禁用元素的事件沿 DOM 树向上传播,因此事件处理程序可以放置在容器元素上。

我可以为此建议小的解决方案。这不是最佳做法,但会解决您的问题:

HTML:

<div style="display:inline-block; position:relative;">
    <input id="productUpdate" name="post[price]" type="text" disabled />
    <div class="inputOverflow" style="position:absolute; left:0; right:0; top:0; bottom:0;"></div>
</div>

jQuery:

$(".inputOverflow").click(function (evt) {
    $(this).hide().prev("input[disabled]").prop("disabled", false).focus();
});​
于 2013-10-26T19:36:30.450 回答
0

你需要使用类似的东西:

$(document).on('click', '#eventDrive', function() {
    alert('test');
});
于 2013-10-26T19:17:35.607 回答