2013-06-26 130 views
4

有一个问题,我有一个表单页面,我用jQuery UI对话框帮助打开。但在这个页面中,我需要在某些对象上使用.click事件,但它不起作用。 这种情况下的代码是来自该页面的简单alert()测试。点击事件不能在jQuery对话框内工作

<input type='text' class='input_date' value='' readonly /> 

所以我希望在点击对话框窗体中的输入框时获得警报。

$('.input_date').click(function() { 
    alert('hi') 
}); 

什么问题,我怎么能得到这个工作?由于

+0

做到把你的脚本之前''标签 – Esailija

回答

4

这是因为你没有,你想在jQuery的时刻到事件绑定到对象执行。

您需要在父容器中绑定事件delegate,或者在加载对话框后使用jQuery的getScript方法。

<div id="dialogContainer" /> 

$('#dialogContainer').on('click', '.input_date', function(){ 
    alert('hi'); 
}); 
+0

嘿交配解释非常感谢,这工作吧!谢谢 ) – Sangsom

0

内的document.ready其他选项:

$(".input_date").keyup(function(){ 
alert('hi'); 
}); 

$(".input_date").change(function(){ 
    alert('hi'); 
}); 
0

把你的点击事件在打开的回调,像这样:

$(".selector").dialog({ 
    open: function(event, ui) { 
     $('.input_date').click(function(){ 
      alert("hi!"); 
     }); 
    } 
}); 
1

如果是动态生成的HTML对话框,你点击的回调不会被连接到“.input_date” elment。使用这个来代替:

$('.input_date').live('click',function() { 
    alert('hi') 
}); 
0

它会正常工作,你只需要设置属性为只读像 这样:

<input type='text' class='input_date' value='' readonly="readonly"> 




     $('.input_date').click(function() { 

       alert('hello'); 
      }); 
0

你还没有从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>