【问题标题】:submit null values in Twitter bootstrap Datepicker在 Twitter 引导程序 Datepicker 中提交空值
【发布时间】:2015-04-16 20:23:54
【问题描述】:

我正在使用 codeigniter 和教义.php,我被困在这个地方,当我没有从 datepicker 中选择任何值时,它应该在数据库中提交空值,但它每次都发送昨天的日期。

function initDatePicker(datePickerID, dateInput){
    $( "#" + datePickerID )

    // Initializing the datepicker
    .datepicker()

    // Detecting the on change date event
    .on('changeDate', function (data) {
      var date = new Date(data.date);
      var dateString = date.getDate() + '-' + parseInt(date.getMonth() + 1) + '-' + date.getFullYear();
      $("#" + dateInput).val(dateString);
    });
}

控制器

 $salesOrder->setDateOfBilling($post_data['date_of_billing']);
  if($date_of_billing)
  {
  $salesOrder->setBillingStatus("Paid");
}
else
{
  $salesOrder->setBillingStatus("Not Paid");
}
  $salesOrder->setDateOfPayment($post_data['date_of_payment']);

  $em->persist($salesOrder);
  $em->flush();

型号

/**
 * @Column(type="datetime", nullable=true)
 */
protected $date_of_billing;

public function getDateOfBilling()
{
    return $this->date_of_billing;
}

public function setDateOfBilling($date_of_billing)
{
    $this->date_of_billing = new \DateTime($date_of_billing);
}

查看 这是我使用日期选择器的形式

<div class="form-group">
<label class="col-sm-4 control-label">Date of Billing</label>
<div class="col-sm-5">
  <input type="text" name="date_of_billing" id="date_of_billing" class="form-control" placeholder="Date Of Billing" 

  readonly>
  <div id="billing_date_picker"></div>
</div>

【问题讨论】:

  • 请把问题说清楚。
  • 问题对我来说很清楚 - 如果 OP 没有选择它选择/发布昨天日期的日期
  • 我们可以看到发送值的 ajax 方法以及您正在使用的控制器代码和模型代码吗?请更新您的问题。还要检查:var date = new Date(data.date);console.log(date); 你会得到什么。
  • 你提醒我Date.getMonth() 从 0 开始。只是...如何??
  • 完成更改仍然发送相同的值......虽然编辑了问题

标签: php jquery twitter-bootstrap codeigniter datepicker


【解决方案1】:

得到上述代码及其工作的控制器和模型的 answer.did 更改...

控制器

$billing_date = $post_data['date_of_billing'] === "" ? null : $post_data['date_of_billing'];
  echo $billing_date;
  $salesOrder->setDateOfBilling($post_data['date_of_billing']);

  $salesOrder->setBillingStatus("Paid");

型号

public function setDateOfBilling($date_of_billing)
{
    if ($date_of_billing == null) {
        $this->date_of_billing = null;
    } else {
        $this->date_of_billing = new \DateTime($date_of_billing);    
    }
}

【讨论】:

    【解决方案2】:

    这里可能有问题“$( "#" + datePickerID )

    试试这个:

    var dpid = "#" + datePickerID;
    $(dpid).datepicker();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      相关资源
      最近更新 更多