2014-02-19 26 views
0

我有這個jQuery代碼。我在這裏以第一行作爲評論,因爲它們在我的問題中並不重要,僅僅用於結構。我點擊事件後,點擊td我有輸入字段中的文字。我將重點放在文字的末尾。但是當我點擊鼠標時,我想刪除焦點,這樣我可以點擊名稱的中間位置,光標就會在那裏。當它是一個('點擊')時它會工作,但我需要多次執行,因此一次點擊只能執行一次。one。('click')reset點擊後

$('td').on('click', function() {  
    //val = $(this).text(); 
    //console.log(val); 
    //rowid = $(this).parents('tr').attr('id'); 
    //realclass = $(this).attr('class'); 
    //$("tr").filter("#" + rowid).find("td").filter("." + realclass).find("span").hide(); //hide td->span field.. 
    //$("tr").filter("#" + rowid).find("td").filter("." + realclass).find("input").show();//..and show input field 
    //get focus on end of input val 
    SearchInput = $("tr").filter("#" + rowid).find("td").filter("." + realclass).find("input"); 
    strLength = SearchInput.val().length; 
    SearchInput.focus(); 
    SearchInput[0].setSelectionRange(strLength, strLength); 
}); 
+0

你能設置一個的jsfiddle重現呢? http://jsfiddle.net/ – RononDex

+2

'$(「tr」)。filter(「#」+ rowid)''可以只是'$(「#」+ rowid)' – Pete

+0

是的。我也錯過了。 – user3316619

回答

2

的問題是,點擊輸入本身也觸發涵蓋<td>元素上的click事件(由於事件傳播或「冒泡」),你不希望發生的事情。爲了防止這種情況,你要輸入處理click事件時調用event.stopPropagation()功能:

$('td input').on('click', function(e) { 
    e.stopPropagation(); 
}); 
+0

是的。謝謝! – user3316619

+0

這是行得通的,除非OP在第一次點擊輸入時也希望這種行爲,但我想不是這種情況。 – plalx