2013-10-26 44 views
0

下面是HTML我有如何註冊點擊事件AJAX加載內容

<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; 
} 

我需要執行click事件被點擊

隱藏輸入框時

這裏是我的js

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

但這裏沒有任何反應。爲什麼會發生這種情況以及如何解決它

+0

「我需要執行click事件被點擊隱藏輸入框時「,不明白,對不起。你怎麼能點擊隱形? – RafH

+0

這就是爲什麼我創建了一個絕對定位輸入的div – overflow

+0

http://jsfiddle.net/yiernehr/B2Bcf/2/ – RafH

回答

0

你需要使用類似:

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

Downvote,因爲這將無法使用禁用的輸入。請檢查一下。 – zur4ik

+0

在OP環境中,「#eventDrive」是「禁用的輸入」嗎? – RafH

+2

如OP所述,當點擊$('#productUpdate')時,他需要觸發$('#eventDrive')。click()'。但是此輸入被禁用,禁用的輸入不會觸發鼠標事件。 – zur4ik

2

要在Ajax加載的內容上發生點擊事件,您必須在容器上註冊事件。我會做這樣的事情:

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

你點擊它會檢查,如果你點擊了#eventDrive這樣的點擊監聽器將在整個文件進行註冊,每一次,即使該元素沒有在註冊聽衆時存在。

+0

真的不明白倒票,這有什麼問題? – tomca32

+1

同樣在這裏,不明白 – RafH

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(); 
});​