简体   繁体   English

捕获输入字段中的文本更改

[英]catch the text change in a input field

I am using joomla calendar to pick a date. 我正在使用joomla日历来选择日期。 Below shows the html code of it. 下面显示了它的html代码。 here when you click the image, calendar will pop up and there you can select a date and popup will close and date value show in input text field. 在此处单击图像时,将弹出日历,您可以在此处选择日期,弹出窗口将关闭,日期值将显示在输入文本字段中。 What I want is I want to write a function to do a ajax post if the date is change.(input text change). 我想要的是如果日期改变,我想写一个函数来做一个ajax帖子。(输入文本更改)。 I tried simple alert() but didn't work. 我尝试过简单的alert()但是没有用。 I think it is due to lost focus on the text field because only the image that we click and no contact with input field with mouse or any key press. 我认为这是由于失去了对文本字段的关注,因为只有我们点击的图像,并且没有用鼠标或任何按键与输入字段联系。 plz help me to solve this. 请帮助我解决这个问题。

$("#select_date").change(function(){
   alert('Working');
});

HTML Code HTML代码

<input id="select_date" type="text" value="2012-02-16" name="select_date" title="Thursday, 16 February 2012">
<img id="select_date_img" class="calendar" alt="Calendar" src="/eap_movies/media/system/images/calendar.png">

PHP Code PHP代码

JHTML::_('behavior.calendar');
JHTML::calendar(date('Y-m-d'),'select_date', 'select_date', '%Y-%m-%d');

Change event will be triggered only when input loose focus (blur). 仅在输入松散焦点(模糊)时才会触发更改事件。

You could watch to keypress, keyup or keydown events if possible or trigger yourself an event when a date is picked. 如果可能,您可以观看按键,键盘或键盘事件,或者在选择日期时触发自己的事件。

Mihai Bazon's DHTML Calendar v1.0 (under /media/system/js/calendar.js) has a callback for onSelected and onClose events. Mihai Bazon的DHTML Calendar v1.0 (在/media/system/js/calendar.js下)onSelectedonClose事件的回调。 So it should be possible to catch when the calendar closes or when the user selects a date. 因此,应该可以在日历关闭或用户选择日期时捕获。

Solution 1 - Editing Joomla 解决方案1 ​​ - 编辑Joomla

Step 1 - You need to override this file: libraries/joomla/html/html.php 第1步 - 您需要覆盖此文件: libraries/joomla/html/html.php

// added $onSelected parameter
public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = null, $onSelected)
{
  ...
$document->addScriptDeclaration('window.addEvent(\'domready\', function() {Calendar.setup({
            inputField: "'.$id.'",          // id of the input field
            ifFormat: "'.$format.'",        // format of the input field
            button: "'.$id.'_img",          // trigger for the calendar (button ID)
            align: "Tl",                            // alignment (defaults to "Bl")
            singleClick: true,
            onSelected: '.$onSelected.'
});});');
...
}

Step 2 - Defining a callback for onSelected 第2步 - 为onSelected定义回调

JHTML::_('behavior.calendar');
JHTML::calendar(date('Y-m-d'),'select_date', 'select_date', '%Y-%m-%d', null, 'userSelectedDate');

Step 3 - Implementing the callback 第3步 - 实现回调

function userSelectedDate(calendar, date){ ... }

Note: I don't have an active install of Joomla so the code above might have syntax errors. 注意:我没有主动安装Joomla,因此上面的代码可能有语法错误。

Solution 2 - Sara's easier solution 解决方案2 - Sara更简单的解决方案

Add onchange definition to the $attribs parameter of JHTML::calendar : onchange定义添加到JHTML::calendar$attribs参数:

JHTML::calendar(date('Ym-d'),'select_date', 'select_date', '%Y-%m-%d','onchange="myfunction();"');

Visit DHTML Calendar Website as well. 访问DHTML日历网站

Which version of jquery do you use ? 你使用哪个版本的jquery? I had the same problem and I went with 我有同样的问题,我去了

$('elementName').on("input propertychange",function() {
 alert("detected");
});

Because working with keyup events will not work in your case because there is no user interaction with the field actually. 因为使用keyup事件在您的情况下不起作用,因为实际上没有用户与该字段的交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM