我正在尝试打开日期选择器对话框。为此,我在 html 输入控件中编写了 inlinde 代码。代码如下所示
<form:input path="sanctionLoans[${count.index}].sanctionDate"
readonly="${param.readOnly}"
style="text-align: right; width: 100px;"
onclick="findMinDate()"
onclick="$(this).datepicker({ dateFormat: 'dd-mm-yy' ,changeMonth: true,changeYear: true }).datepicker('show');"/>
此代码工作正常并打开对话框。但现在我想将此代码编写为函数,以便我可以处理其他事件,例如 onselect、onclose 等。为此,我编写了一个类似这样的函数。
function findMinDate(event){
var id = $(event).attr('id');
console.log(id);
id = id.replace(/\./g, "\\\\.");
var ele = '#' + id;
console.log(ele);
$("#sanctionLoans1\\.sanctionDate").datepicker({
dateFormat: 'dd-mm-yy' ,
changeMonth: true,
changeYear: true
onSelect: function(endDate) {
}
})
.datepicker('show');
}
现在这个函数有问题,因为输入控件 ID 包含句点运算符(.),所以首先将其替换为“\.”。但是将 .(点) 替换为\后。它不会打开对话框。但如果硬核称其为“#sanctionLoans0\.sanctionDate”,那么它工作正常。 有人可以帮忙解决功能上的问题吗?
最佳答案
这个
var id = $(event).attr('id');
应该是
var id = $(event.target).attr('id'); //or event.target.id
id = id.replace(/\./g, "\\.");
当您将变量传递给选择器时。changeYear: true
错过了,
此外,我看到您已经有了 onclick="$(this).datepicker({ dateFormat: 'dd-mm-yy' ,changeMonth: true,changeYear: true }).datepicker('show') ;"
,这样就不需要调用内联函数了。
<强> Fiddle Demo
关于javascript - 当 id 具有句点运算符时启用打开日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152687/