2010-12-11 5 views
0

여러 개의 텍스트 입력과 여러 개의 버튼이있는 페이지가 있습니다. 내가 버튼을 클릭하면 그 버튼의 값은 캐럿에 중 필드 캐럿,에 삽입하려면 여기에 지금까지 내 함수 호출은 다음과 같습니다.jQuery : 선택한 텍스트 상자에 캐럿이 있음/찾을 수 없음

$('li input[type="button"]').click(function(){$('#aTextInput').insertAtCaret($(this).val())}); 

내가 실제로 입력과 $('#aTextInput')을 대체 할 그 안에 캐럿이 있습니다. 어떤 아이디어?

(. 누군가가 요청하기 전에 Here'sinsertAtCaret 기능, 당신은 아마 당신이 궁금하지 않는 한 그것을보고하지 필요성을한다.)

편집 :

그래서 나는이 시도 :

jQuery.extend(jQuery.expr[':'], { 
    focus: "a == document.activeElement" 
}); 

$('li input[type="button"]').click(function(){$('input:focus').insertAtCaret($(this).val())}); 

하지만 버튼을 클릭해도 아무런 변화가 없습니다. this solution을 잘못 구현하고 있습니까?

+0

http://stackoverflow.com/questions/2683742/is-there- a-has-focus-in-javascript-or-jquery/2683838 # 2683838 – charliegriefer

+0

나는 그것을 이미 보았지만, 제대로 작동하지는 않습니다. 내 질문을 편집하여 구현 방법을 보여줄 것입니다. 아마도 잘못된 것일 수도 있습니다. – user460847

답변

3

텍스트 영역이 여러 개인 경우 다음과 같이하십시오. 버튼 텍스트 영역을 클릭

// any time a textarea gets focus, set the ID 
$('textarea').focus(function(){ 
    focusID = $(this).attr('id'); 
}); 

// when the button is clicked 
$('input[type="button"]').click(function(){  
    // insert the value into the textarea 
    insertAtCaret(focusID,$(this).val()) 
}); 

Check out my example on jsFiddle here

내가 가진 문제는했다가,이 버튼을 클릭 한 순간에 집중했다 텍스트 영역을 찾기 힘들었다 있도록 초점을 잃는다.

편집

난 그냥 당신이 입력이 작업을 수행하기 위해 노력하고 있다는 것을 깨달았다

, 여기에 새로운 jsFiddle : http://jsfiddle.net/tVNDL/1/

// any time a input gets focus, set the ID 
$('input').focus(function(){ 
    focusID = $(this).attr('id'); 
}); 

// when the button is clicked 
$('input[type="button"]').click(function(){  
    // insert the value into the textarea 
    insertAtCaret(focusID,$(this).val()) 
}); 
+0

그게 전부입니다! 정말 고마워! – user460847

+0

내가 도울 수있어서 기쁘다. :-) – jyoseph

관련 문제