我有一个问题,但我没有了,谢谢
或者你可以拥有
$("form").on('change', "#startDate, #endDate", function(event){...})
所以你不需要担心更换元素
要将更改事件附加到多个元素,您可以使用多重选择器。另一种可能性是使用 类选择器
在事件处理程序内部,要获取触发事件的元素,您可以使用 event.target。
$(function () {
// Multiple selector:
// $('#startDate, #endDate').datepicker();
// or, class selector:
$(':text.dpClass').datepicker();
//$('#startDate, #endDate').on('change', function(e) {
$(':text.dpClass').on('change', function(e) {
$('#log').text('Changed element is:' + e.target.id);
});
});
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<p id="log"></p>
<form class="uk-form">
<label for="startDate">Start Date:</label>
<input type="text" id="startDate" class="dpClass" data-uk-datepicker="{format:'YYYY-MM-DD'}">
<label for="endDate">End Date:</label>
<input type="text" id="endDate" class="dpClass" data-uk-datepicker="{format:'YYYY-MM-DD'}">
</form>
尝试:
$('input').on('change', function () {
当然,如果您在页面上有更多输入,它也会为它们触发。因此,也许可以为两个选择器提供唯一的类,并向该类添加
on
处理程序。
为两个输入命名。试试这个:
$("input[name='someName']").on('change', function() {});