簡體   English   中英

Click事件在jQuery對話框中不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM