将两个文本框与jquery中的datetime值进行比较

时间:2017-03-10 08:37:07

标签: javascript jquery asp.net input textbox

我有两个文本框,一个是到达,另一个是离开。如果用户尝试输入例如。:

" 2017年3月9日"在出发和抵达日期有" 2017-03-10",我希望出发价值与抵达时相同。

由于某些原因,我的代码目前无效。

jQuery的:

 public function addProductAction(Request $request) {
    $create = new Products();
    $images = new Images();
    $form = $this->createForm(ProductsForm::class, $create);

    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()) {
        $title = $form['title']->getData();
        $category = $form['category']->getData()->getId();
        $count = $form['count']->getData();
        $size = $form['size']->getData();
        $price = $form['price']->getData();
        $slug = $title;
        $image = $form['image']->getData();
        $brand = $form['brand']->getData();

        $zdjecia = $create->getImage();

        print_r($image);
    }
 }

2 个答案:

答案 0 :(得分:1)

这样的东西?

 function onChange(sender, txt, departed) 
 {
    var txtArrival = $("#txtArrivalDate");
    var txtArrivalDate = $(txtArrival).val(); //Value from arrival
    var txtDate = $(txt).val(); //Value from departed
    
    var departureDate = new Date(txtDate); //Converting string to date
    var arrivalDate = new Date(txtArrivalDate); //Converting string to date

    

    if (departureDate.getTime() < arrivalDate.getTime()) {
         txt.val(txtArrivalDate); //Does not work, value is not updated
    }
}

$("#txtDepartureDate").change(function(event){
  onChange(event, $("#txtDepartureDate"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="date" id="txtArrivalDate" />
<input type="date" id="txtDepartureDate" />

答案 1 :(得分:-2)

$(txt).val(txtArrivalDate);

$(txt).html(txtArrivalDate);

而不是

txt.val = txtArrivalDate;

你想要实现的目标?