javascript 如何在textarea中触发点击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27424621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger click event in textarea?
提问by Santhucool
How to trigger click event in textarea?
如何在textarea中触发点击事件?
I tried:
我试过:
jquery
查询
$(document).ready(function(){
$('#txtid').val('something');
$('#txtid').trigger('click');
});
html
html
<textarea id='txtid' rows='5' cols='20'></teaxtarea>
But the click is not happened. What is wrong in the code.
但是点击没有发生。代码有什么问题。
I tried the focus event like this
我试过这样的焦点事件
$('#txtid').focus(); //this worked
But i need to trigger a click event after getting the value to textarea.
但是我需要在将值获取到 textarea 后触发点击事件。
Please help..
请帮忙..
采纳答案by Mandalordevelopment
You have to set a click event handler first.
您必须先设置点击事件处理程序。
Edit 1: Since you try to trigger the click event within document ready, you have to declare the click event handler within the document ready event handler, even before you trigger it.
编辑 1:由于您尝试在文档就绪内触发单击事件,因此您必须在文档就绪事件处理程序中声明单击事件处理程序,甚至在触发它之前也是如此。
Html:
网址:
<textarea id='txtid' rows='5' cols='20'></textarea>
jQuery:
jQuery:
$(document).ready(function(){
$('#txtid').click(function() { alert('clicked'); });
$('#txtid').val('something');
$('#txtid').trigger('click');
});
Fiddle: http://jsfiddle.net/j03n46bf/
小提琴:http: //jsfiddle.net/j03n46bf/
Edit 2:
编辑2:
Since you want the event to be triggered after you get a value to your textarea, Milind Anantwaris right, you have to use the onchange event:
由于您希望在获得 textarea 值后触发事件,Milind Anantwar是对的,您必须使用 onchange 事件:
Same Html, different jQuery:
相同的 Html,不同的 jQuery:
$(document).ready(function(){
$('#txtid').change(function() { alert('Value changed'); });
$('#txtid').val('something');
$('#txtid').trigger('change');
});
Fiddle: http://jsfiddle.net/sb2pohan/
小提琴:http: //jsfiddle.net/sb2pohan/
Edit 3:
编辑3:
After some comments:
经过一些评论:
$(document).ready(function(){
$('#txtid').click(function() { changeDiv(); });
$('#txtid').focus(); // Set Focus on Textarea (now caret appears)
$('#txtid').val('something'); // Fill content (caret will be moved to the end of value)
$('#txtid').trigger('click'); // Click Handler will be executed
});
function changeDiv() {
// do some stuff;
}
Edit 4: Working test.html (Tested in IE, FF, Chrome):
编辑 4:工作 test.html(在 IE、FF、Chrome 中测试):
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#txtid").click(function() { changeDiv(); });
$("#txtid").focus();
$("#txtid").val("value");
$("#txtid").trigger("click");
});
function changeDiv() {
$('#changeDiv').css("background-color","red");
$('#changeDiv').html("changed content");
}
</script>
</head>
<body>
<textarea id="txtid"></textarea>
<div id="changeDiv" style="background-color: green; width: 100px; height: 100px;">start content</div>
</body>
</html>
回答by Milind Anantwar
You probable need to trigger change event:
您可能需要触发更改事件:
$('#txtid').trigger('change');
回答by Wilfredo P
As the documentationsays, the event click/Change needs exits to that works so your Javascrtipt should be:
正如文档所说,事件单击/更改需要退出才能正常工作,因此您的 Javascrtipt 应该是:
$(document).ready(function(){
$('#txtid').val('something');
$('#txtid').on('change', function(){
alert('hi!!!!!');
})
$('#txtid').trigger('change');
});
回答by Emin
To see it works first define click listener, then trigger the click event.
要查看它的工作原理,首先定义单击侦听器,然后触发单击事件。
$(document).ready(function(){
$('#txtid').val('something');
$('#txtid').click(function(){alert("clicked");}); //define click listener
$('#txtid').click(); //click text area
});
回答by Victor Stoddard
Many of these solutions were sufficient years ago, but HTML5's MAXLENGTH
property works on every browser I've tested. It works with keydown/upand pastingwith mouse or keyboard. It also doesn't give you the disappearing final character as you type. Javascript shouldn't be needed.
许多这些解决方案在几年前就足够了,但 HTML5 的MAXLENGTH
属性适用于我测试过的每个浏览器。它适用于keydown/up和鼠标或键盘粘贴。它也不会在您键入时为您提供消失的最终字符。不需要 Javascript。
回答by Phil Tune
Simply $('#txtid').click();
will trigger the click event.
只需$('#txtid').click();
将触发click事件。