最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Click event doesn't work inside jQuery dialog - Stack Overflow

programmeradmin0浏览0评论

got a problem that I have a form page which I open with jQuery UI dialog help. But in this page I have need to use .click event on some objects, but it doesn't work. The code for this case is simple alert() test from that page.

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

So I want to get alert when I click on input field inside that dialog form.

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

What's the problem, how can I get this work? Thanks

got a problem that I have a form page which I open with jQuery UI dialog help. But in this page I have need to use .click event on some objects, but it doesn't work. The code for this case is simple alert() test from that page.

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

So I want to get alert when I click on input field inside that dialog form.

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

What's the problem, how can I get this work? Thanks

Share Improve this question asked Jun 26, 2013 at 11:10 SangsomSangsom 2576 silver badges15 bronze badges 1
  • Put your scripts just before </body> tag – Esailija Commented Jun 26, 2013 at 11:13
Add a ment  | 

6 Answers 6

Reset to default 4

This is because you don't have the object you want to bind the event to at the moment of the jquery execution.

You need to delegate the event binding in a parent container or use the getScript jquery method after your dialog is loaded.

<div id="dialogContainer" />

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

If the dialog html is generated dynamically, your click callback wont be attached to the ".input_date" elment. Use this instead:

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

Other options inside document.ready :

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

or

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

Put your click event in the open callback, like this:

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

It will work fine you just need to set attribute to readonly like this:

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




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

              alert('hello');
          });

You have not given document ready function from jquery. You can do it by

$(function() {
// your code
});

Sample for your program

<html>
<head>
<title>Untitled</title>
<script src="http://code.jquery./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>
发布评论

评论列表(0)

  1. 暂无评论