我有一个包含多个文本输入和多个按钮的页面。当我单击按钮时,我希望将该按钮的值插入到插入符号处,无论插入符号位于哪个字段。这是迄今为止我的函数调用:
$('li input[type="button"]').click(function(){$('#aTextInput').insertAtCaret($(this).val())});
我想将 $('#aTextInput')
替换为实际包含插入符号的输入。有什么想法吗?
(Here's insertAtCaret
函数,在任何人询问之前。您可能不需要需要查看它,除非您很好奇。)
编辑:
所以我尝试了这个:
jQuery.extend(jQuery.expr[':'], {
focus: "a == document.activeElement"
});
$('li input[type="button"]').click(function(){$('input:focus').insertAtCaret($(this).val())});
但是当我单击按钮时,什么也没有发生。我正在实现this solution错了?
最佳答案
如果您有多个文本区域,您可能会想要执行类似的操作。
// 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())
});
关于javascript - jQuery:查找哪个文本框有插入符号/被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4415874/