输入不会在更改时提交表单

时间:2016-07-08 16:23:26

标签: javascript jquery date input datepicker

我在"有变化时遇到问题"输入。 它应该做的是点击它显示datepicker,但是当你选择提交日期时,输入更改功能不起作用。 请在下面运行代码段。



if (top.location != location) {
  top.location.href = document.location.href;
}
$(function() {
  window.prettyPrint && prettyPrint();
  $('#dp1').datepicker({
    format: 'yyyy-mm-dd'
  });

  $('#dpYears').datepicker();
  $('#dpMonths').datepicker();
});

<link href="http://www.eyecon.ro/bootstrap-datepicker/css/bootstrap.css" rel="stylesheet" />
<link href="http://www.eyecon.ro/bootstrap-datepicker/css/datepicker.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.eyecon.ro/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>

<div data-date-format="yyyy-mm-dd">
  <form id="date" method="POST" action="index.php">
    <input type="text" name="date" class="span2" value="2016-08-07" id="dp1" style="text-align: center; width: 50%; background-color: #5a95f5; border: none;">
  </form>
</div>

<script>
  $('input[id="dp1"]').change(function() {
    $('#date1').submit();
  });
</script>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

问题是当您选择日期时,您的输入不会触发change()功能。你可以做的是使用datepicker的事件,在这种情况下是changeDate

&#13;
&#13;
if (top.location != location) {
  top.location.href = document.location.href;
}
$(function() {
  window.prettyPrint && prettyPrint();
  $('#dp1').datepicker({
    format: 'yyyy-mm-dd'
  });

  $('#dpYears').datepicker();
  $('#dpMonths').datepicker();
});
&#13;
<link href="http://www.eyecon.ro/bootstrap-datepicker/css/bootstrap.css" rel="stylesheet" />
<link href="http://www.eyecon.ro/bootstrap-datepicker/css/datepicker.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.eyecon.ro/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>

<div data-date-format="yyyy-mm-dd">
  <form id="date" method="POST" action="index.php">
    <input type="text" name="date" class="span2" value="2016-08-07" id="dp1" style="text-align: center; width: 50%; background-color: #5a95f5; border: none;">
  </form>
</div>

<script>
  $('#dp1').on('changeDate', function () {
    console.log('Date Selected')
  })
</script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

简单,您可以使用此代码:

$('#input-date')
.datepicker()   
.on('changeDate', function(e){    
    $('#formID').submit();
});
相关问题