2013-10-30 116 views
2

我想要防止輸入焦點onclick和焦點輸入雙擊。雙擊焦點輸入?

喜歡的東西....

$('input').click(function) { 
    $(this)preventFocus(); 
}); 

$('input').dblclick(function) { 
    $(this)focus(); 
}); 

或者,也許我應該使用if語句?

+1

試$(本).blur()而不是$(本)preventFocus() –

+0

你有沒有真的試圖什麼嗎?檢查出:http://stackoverflow.com/questions/8735764/prevent-firing-focus-event-when-clicking-on-div –

+0

一直嘗試2小時隊友:(我會看看這個答案,謝謝 – colmtuite

回答

5

我想這樣的事情應該工作:

$('input').on({ 
    focus: function() { 
     if (!$(this).data('disabled')) this.blur() 
    }, 
    dblclick: function() { 
     $(this).data('disabled', true); 
     this.focus() 
    }, 
    blur: function() { 
     $(this).data('disabled', false); 
    } 
}); 

FIDDLE

+0

完美,謝謝隊友:) – colmtuite

+0

看起來不錯。嘗試了淘汰賽,但顯然有干擾。 –

1

你可以嘗試像下面

HTML:

<input type="text" id="myInput" readonly="readonly" /> 

的jQuery:

$("#myInput").dblclick(function(){ 
    $(this).prop("readonly", false) 
}); 
$("#myInput").blur(function(){ 
    $(this).prop("readonly", true); 
}); 

http://jsfiddle.net/KWuR5/