【问题标题】:How to add date value in dynamically created date field using Jquery如何使用 Jquery 在动态创建的日期字段中添加日期值
【发布时间】:2018-11-01 06:06:37
【问题描述】:

我需要一个帮助。我有一个json array,我需要在使用Jquery 动态创建的日期字段中添加一些日期值存在于JSON array 中。我在下面解释我的代码。

$depdate=[  
   {  
      "sett_id":"270",
      "sett_name":"Car",
      "sett_type":"vtypes",
      "id":"6",
      "dep_dates":"31-10-2018,07-11-2018,14-11-2018,21-11-2018,28-11-2018",
      "vehicle_type":"270"
   },
   {  
      "sett_id":"271",
      "sett_name":"Bus",
      "sett_type":"vtypes",
      "id":"7",
      "dep_dates":"01-11-2018,08-11-2018,22-11-2018,15-11-2018,29-11-2018,06-12-2018",
      "vehicle_type":"271"
   }] 

这是我的 JSON array 值,它有键 dep_dates,它包含所有用逗号分隔的日期。我正在解释下面的代码。

<?php foreach ($depdate as $key => $value) { 
                $dep_date=explode(",",$value['dep_dates']);
                //print_r($dep_date);exit;
              ?>
              <tr>
                <td><?php echo $key+1; ?></td>
                <td><?php echo $value['sett_name']; ?></td>
                <td><div class="input_fields_wrap<?php echo $key+1; ?> form-horizontal"><div style="margin-bottom: 5px;"><input type="text" name="ddate_<?php echo $value['sett_id']; ?>[]" value="" class="depdate<?php echo $key+1; ?> form-control"  style="width:280px;max-width:280px;display:inline-block"><button type="button" class="add_field_button<?php echo $key+1; ?> btn btn-primary" style="margin-left: 2px; margin-top: -5px;min-height: 30px;"><i class="fa fa-plus"></i></button></div></div></td>
                <input type="hidden" name="vid_<?php echo $value['sett_id']; ?>_id" value="<?php echo $value['sett_id']; ?>" >
              </tr>
              <script>
                $(function(){
                  var addButton = $('.add_field_button<?php echo $key+1; ?>');
                  var wrapper = $('.input_fields_wrap<?php echo $key+1; ?>');
                  var nowTemp = new Date();
                  var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
                  $(addButton).click(function(){
                    $(wrapper).append('<div style="margin-bottom: 5px;"><input type="text" name="ddate_<?php echo $value['sett_id']; ?>[]" value="" class="depdate<?php echo $key+1; ?> form-control"  style="width:280px;max-width:280px;display:inline-block"/><button type="button" class="remove_field<?php echo $key+1; ?> btn btn-danger" style="margin-left: 2px; margin-top: -5px;min-height: 30px;"><i class="fa fa-minus"></i></button></div>'); //add input box
                    $('.depdate<?php echo $key+1; ?>').datepicker({
                      format: 'dd-mm-yyyy',
                      onRender: function(date) { 
                        return date.valueOf() < now.valueOf() ? 'disabled' : ''; 
                      } 
                    });
                  })
                  $(wrapper).on('click', '.remove_field<?php echo $key+1; ?>', function(e){
                    e.preventDefault();
                    $(this).parent('div').remove();
                  })
                  $('.depdate<?php echo $key+1; ?>').datepicker({
                    format: 'dd-mm-yyyy',
                    onRender: function(date) { 
                      return date.valueOf() < now.valueOf() ? 'disabled' : ''; 
                    }
                  });
                })
              </script>
              <?php } ?>

这里我有一个带有+ 按钮的日期字段,当用户单击它时,正在创建新字段。这里我的要求是如果dep_dates 有任何值,它将转换为数组,并且日期字段将根据日期值动态创建,并且这些值将附加到字段中。实际上这是update 功能。请帮我做这件事。

【问题讨论】:

    标签: javascript php jquery jquery-ui-datepicker


    【解决方案1】:

    您快接近了,您想将数据从$depdate json 填充到datepicker 文本框。

    解决方案:

    1. 从 PHP 中获取 sett_id 并读取相同的 json 值
    2. dep_dates转换成数组
    3. 将该数组的值设置为动态应用的总计 datepicker

    请检查下面的 HTML 代码(阅读评论行):

    $(document).ready(function () {
        $depdate = [
        {
            "sett_id": "270",
            "sett_name": "Car",
            "sett_type": "vtypes",
            "id": "6",
            "dep_dates": "31-10-2018,07-11-2018,14-11-2018,21-11-2018,28-11-2018",
            "vehicle_type": "270"
        },
        {
            "sett_id": "271",
            "sett_name": "Bus",
            "sett_type": "vtypes",
            "id": "7",
            "dep_dates": "01-11-2018,08-11-2018,22-11-2018,15-11-2018,29-11-2018,06-12-2018",
            "vehicle_type": "271"
        }]
    });
    
    $(function () {
        var addButton = $('.add_field_button1');
        var wrapper = $('.input_fields_wrap1');
        var nowTemp = new Date();
        var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
                
        $(addButton).click(function () {
            var sett_id = 0;///Apply PHP code here
            var myDatesArr = $depdate[sett_id].dep_dates.split(',');///Read json file and convert date into array
            var crrDatePosition = $('.input_fields_wrap1 .datepicker').length;//Get total existing datepicker
            $(wrapper).append('<div style="margin-bottom: 5px;"><input type="text" name="ddate_' + sett_id + '" value="' + myDatesArr[crrDatePosition] + '" class="depdate1 datepicker form-control"  style="width:280px;max-width:280px;display:inline-block"/><button type="button" class="remove_field1 btn btn-danger" style="margin-left: 2px; margin-top: -5px;min-height: 30px;">-</button></div>'); //add input box
            $('.depdate1').datepicker({
                format: 'dd-mm-yyyy',
                onRender: function (date) {
                    return date.valueOf() < now.valueOf() ? 'disabled' : '';
                }
            });
        })
        $(wrapper).on('click', '.remove_field1', function (e) {
            e.preventDefault();
            $(this).parent('div').remove();
        })
        $('.depdate1').datepicker({
            format: 'dd-mm-yyyy',
            onRender: function (date) {
                return date.valueOf() < now.valueOf() ? 'disabled' : '';
            }
        });
    })
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.5/datepicker.css" />
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.5/datepicker.js"></script>
    
    <table border="1">
        <tr>
            <td>1</td>
            <td>Something <input type="hidden" name="vid_1_id" value="1"></td>
            <td>
                <div class="input_fields_wrap1 form-horizontal">
                    <div style="margin-bottom: 5px;">
                        <input type="text" name="ddate_1" value="" class="depdate1 form-control" style="width:280px;max-width:280px;display:inline-block">
                        <button type="button" class="add_field_button1 btn btn-primary" style="margin-left: 2px; margin-top: -5px;min-height: 30px;">Click here to add</button>
                    </div>
                </div>
            </td>
        </tr>
    </table>

    如您所见,var sett_id = 0; 静态应用为零,您需要更改其 PHP 值。

    【讨论】:

    • 它是一种解决方案,但它可能会给用户带来混淆,因为它是一种“更新功能”。
    猜你喜欢
    • 2019-03-11
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    相关资源
    最近更新 更多