簡體   English   中英

當按下“完成”按鈕時,datetimepicker觸發javascript函數

[英]datetimepicker trigger a javascript function when the 'Done' button is pressed

我從這里取了一個日期時間選擇器。 此小部件可讓您從日歷中選擇一天並設置時間。 還有一個“完成”按鈕,它關閉小部件並設置日期。 按下完成按鈕后,我需要能夠從當前腳本中調用javascript函數。 我嘗試了不同的解決方案,但沒有一個起作用:

my_page.html:

<link rel=stylesheet href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script src="timepicker/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript">
\$(function() { \$('#selectedDateTime').datetimepicker(); });
</script>
...
<input type="text" id="selectedDateTime">

我已經嘗試過了,但是沒有用:

<script type="text/javascript">
$("#selectedDateTime").datetimepicker({
onClose: function(dateText, inst) {
   my_func();
  }
});
function my_func() {
  alert('my_func triggered');
}
</script>

我已經設法在更改日期或時間時調用函數,但是我只想在按下完成按鈕時調用函數。 這樣,我避免多次調用該函數。

<input type="text" id="selectedDateTime" onChange="javascript:my_func"> // this is working, but calls the function all the times, even if the button is not pressed

我從這里找到了這個解決方案

<script type="text/javascript">
 function isDonePressed(){
   return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
}

function my_func() {
  if(isDonePressed())
    alert('my_func triggered');
}

你不能只是...

$(document).on('click', 'THE-BUTTON-ID/CLASS', function(){
    alert('my_func triggered');
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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