查找并替换输入字段中的文本


Find and replace text in an input field

我有一个joomla注册表(使用easyprofile joomla组件),其中有一个日期字段(日历日期选择器),将日期按以下格式显示:

2016年12月31日

我想把这个日期的格式改成:

2016年12月31日

我想知道php甚至js是否可以找到并替换该字段中的字符串,如下所示:

查找:-01-更换:-1月-

查找:-02-更换:-2月-

一旦用户从日历中选择或更改日期,这就必须立即生效。

非常感谢任何人都能帮忙!

尝试以下操作:

拆分字符串,按索引查找月份,重新创建日期并添加为值。

[JQUERY]

var months = ['Jan','Feb','May','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
// You can also use the classname or id to select the element.
// var element = $('.classname');
var element = $('input[name="jform[moving_date]"]');
element.on('change', function(e) {
    // You might need to stop propagation but this will cause other scripts from firing like date picker.
    // e.stopPropagation();
    var date_parts = $(this).val().split('-');
    var month_index = Number(date_parts[1]) - 1;
    var date_new = date_parts[0] + '-' + months[month_index] + '-' + date_parts[2];
    $(this).val(date_new);
});