![](/img/trans.png)
[英]Click Event inside of a jQuery Plugin on another element doesn't work
[英]Click event doesn't work inside jQuery dialog
有一个问题,我有一个表单页面,我用jQuery UI对话框帮助打开。 但是在这个页面中,我需要在某些对象上使用.click事件,但它不起作用。 这种情况的代码是来自该页面的简单alert()测试。
<input type='text' class='input_date' value='' readonly />
所以当我点击该对话框表单中的输入字段时,我想要提醒。
$('.input_date').click(function() {
alert('hi')
});
问题是什么,我怎样才能做到这一点? 谢谢
这是因为在jquery执行时,您没有要将事件绑定到的对象。
您需要在父容器中delegate
事件绑定,或者在加载对话框后使用getScript
jquery方法。
<div id="dialogContainer" />
$('#dialogContainer').on('click', '.input_date', function(){
alert('hi');
});
如果动态生成对话框html,则单击回调不会附加到“.input_date”元素。 请改用:
$('.input_date').live('click',function() {
alert('hi')
});
document.ready中的其他选项:
$(".input_date").keyup(function(){
alert('hi');
});
要么
$(".input_date").change(function(){
alert('hi');
});
将您的click事件放在open回调中,如下所示:
$( ".selector" ).dialog({
open: function( event, ui ) {
$('.input_date').click(function(){
alert("hi!");
});
}
});
它会正常工作你需要将属性设置为readonly像这样:
<input type='text' class='input_date' value='' readonly="readonly">
$('.input_date').click(function () {
alert('hello');
});
你没有从jquery给出文件就绪功能。 你可以做到
$(function() {
// your code
});
您的计划的示例
<html>
<head>
<title>Untitled</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function() {
$(".input_date").bind("click", function(){
alert("hi");
});
});
</script>
</head>
<body>
<input type='text' class='input_date' value='' readonly />
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.