使用php codeigniter将计算的javascript变量值传递给文本字段

时间:2017-11-22 11:45:29

标签: javascript php jquery codeigniter

我尝试将javascript变量值提取到文本字段,但我收到了错误。

  

[object Object]

enter image description here

我试图计算两个日期之间的差异,并希望在文本字段中显示,之后想要将其保存在数据库中。

当我在span选项卡中显示结果时,它会给我正确的结果。 enter image description here

这是我的代码:查看文件

<div class="col-md-3">
                <div class="form-group">
                    <div class="col-md-11"> 
                        <div class='input-group date'  name="exam_date"  id='from_date' data-date="" data-date-format="yyyy-mm-dd">
                            <input type='text' class="form-control" id="datepicker1" id="yes" name="property_gas_issue_date" placeholder="Issue Date " />
                            <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <div class="col-md-11">
                        <div class='input-group date'  name="exam_date"  id='from_date' data-date="" data-date-format="yyyy-mm-dd">
                            <input type="text" class="form-control" id="datepicker8" name="property_gas_expiry_date" placeholder="Gas expiry date"/>
                            <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <div class="col-md-11">
                        <!-- <input type="text" id="diff" name="property_gas_certificate_duration_days" placeholder="duration in days" value="" class="form-control"> -->
                        <strong><span> Renewal days count:</span> <span id='diff'> - </span> <span> Days</span></strong>
                    </div>
                </div>
            </div>

注意:在上面的代码中,我只是评论文本框代码。

JS代码:

<script>
  $('#datepicker1').datepicker();
  $('#datepicker8').datepicker();

  $('#datepicker8').change(function () {
      var diff = $('#datepicker1').datepicker("getDate") - $('#datepicker8').datepicker("getDate");
      $('#diff').text(diff / (1000 * 60 * 60 * 24) * -1);
  });
</script>

这个文本框字段的JS代码:

<script>
  $('#datepicker1').datepicker();
  $('#datepicker8').datepicker();

  $('#datepicker8').change(function () {
      var diff = $('#datepicker1').datepicker("getDate") - $('#datepicker8').datepicker("getDate");
      var result = $('#diff').text(diff / (1000 * 60 * 60 * 24) * -1);

      document.getElementById("diff").value = result;
  });
</script>

欢迎任何形式的帮助,提前谢谢。

更新了问题:

这是我的代码:

<script>

  var date1 = new Date();
  date1.setMonth(date1.getMonth() + 12);
  $('#datepicker4').datepicker("setDate", new Date());
  $('#datepicker5').datepicker("setDate", date1);

  //$('#datepicker4').datepicker();
  $("#datepicker4").datepicker({ onSelect: function(dateText) { var dateofDatePicker1 = dateText ;
  $('#datepicker5').trigger('change'); } });

  $('#datepicker5').datepicker();
  $('#datepicker5').change(function () {
      var gasDiff= $('#datepicker5').datepicker("getDate") - $('#datepicker4').datepicker("getDate");
      var result = gasDiff / (1000 * 60 * 60 * 24) * -1;

      document.getElementById("gasDiff").value = result;
  });
</script>

输出: enter image description here

在上图中,结果文本框为空。

1 个答案:

答案 0 :(得分:1)

在这里更新结果变量,试试吧

 <script>
    $('#datepicker1').datepicker();
    $('#datepicker8').datepicker();

    $('#datepicker8').change(function () {
        var diff = $('#datepicker1').datepicker("getDate") - $('#datepicker8').datepicker("getDate");
        var result =  diff / (1000 * 60 * 60 * 24) * -1 ;

        document.getElementById("diff").value = result;
    });
</script>