【问题标题】:Input does not submit the form on change输入不提交更改表单
【发布时间】:2023-03-26 15:07:02
【问题描述】:

我对“on change”输入有疑问。 它应该做的是单击它会显示日期选择器,但是当您选择要提交的日期时,输入更改功能不起作用。 请在下面运行代码 sn-p。

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>

【问题讨论】:

  • 你在#date1上打电话给submit()?不应该是#date吗?

标签: javascript jquery date input datepicker


【解决方案1】:

简单的你可以使用这个代码:

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

【讨论】:

    【解决方案2】:

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

    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>
      $('#dp1').on('changeDate', function () {
        console.log('Date Selected')
      })
    </script>

    【讨论】:

    • 哇,它有效。我刚刚将 console.log('Date Selected') 替换为 $('#date').submit();非常感谢!
    • 很高兴帮助你 @joni
    猜你喜欢
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    相关资源
    最近更新 更多