Javascript 如何在html输入中获取jquery数组

Javascript 如何在html输入中获取jquery数组,javascript,php,jquery,codeigniter,Javascript,Php,Jquery,Codeigniter,我试图将jquery数组放入html输入中,但我不知道我错在哪里 HTML: <input type="hidden" id="datepicker" name="dates[]" /> <script> $('#datepicker').datepick({ dateFormat: 'yyyy-mm-dd', multiSelect: '100', //convert the selected date into arr

我试图将jquery数组放入html输入中,但我不知道我错在哪里

HTML:

<input type="hidden" id="datepicker" name="dates[]" />
<script>
    $('#datepicker').datepick({

      dateFormat: 'yyyy-mm-dd',
      multiSelect: '100',

      //convert the selected date into array and assign the value to your hidden filed when the date picker will close by any format

      onClose: function(dates) { 
         var selectedDate = []; 
         for (var i = 0; i < dates.length; i++) { 
             selectedDate.push($.datepick.formatDate(dates[i])); 
         } 
         $('#datepicker').val(JSON.stringify(selectedDate));
      },

   });
</script>
试试这个

 // if $dates = ['2018-10-9','2018-10-10']
$dates= $this->input->post('dates');
if(count($dates)!=0){
  $isDate = "";
  for($i=0; $i<=count($dates); $i++) {
    $isDate .= $dates[$i].',';
  }
  $isDate = rtrim($isDate,',');
  print_r($isDate);
  // will return $isDate = "'2018-10-9','2018-10-10'";
} else {
  print_r("no dates");
}
//如果$dates=['2018-10-9','2018-10-10']
$dates=$this->input->post('dates');
如果(计数($dates)!=0){
$isDate=“”;
因为($i=0;$i得到了答案。
HTML:

<script>
$('#datepicker').datepick({
    dateFormat: 'yyyy-mm-dd',
    multiSelect: '100'
});
$(document).ready(function() {
    $("#butto").click(function() {
        var dates = $('#datepicker').datepick('getDate');
        console.log(dates);

        $('#datepicker').val(dates);
    });
});
</script>
$dates= $this->input->post('dates');

foreach($dates as $datee) {
    print_r($datee);
}
*<input type="hidden" id="datepicker1" name="dates[]" />*
<script>
$('#datepicker').datepick({
    dateFormat: 'yyyy-mm-dd',
    multiSelect: '100'
});
$(document).ready(function() {
    $("#butto").click(function() {
        var dates = $('#datepicker').datepick('getDate');
        console.log(dates);

        *$('#datepicker1').val(dates);*
    });
});
</script>
**
Jquery:

<script>
$('#datepicker').datepick({
    dateFormat: 'yyyy-mm-dd',
    multiSelect: '100'
});
$(document).ready(function() {
    $("#butto").click(function() {
        var dates = $('#datepicker').datepick('getDate');
        console.log(dates);

        $('#datepicker').val(dates);
    });
});
</script>
$dates= $this->input->post('dates');

foreach($dates as $datee) {
    print_r($datee);
}
*<input type="hidden" id="datepicker1" name="dates[]" />*
<script>
$('#datepicker').datepick({
    dateFormat: 'yyyy-mm-dd',
    multiSelect: '100'
});
$(document).ready(function() {
    $("#butto").click(function() {
        var dates = $('#datepicker').datepick('getDate');
        console.log(dates);

        *$('#datepicker1').val(dates);*
    });
});
</script>

$(“#日期选取器”)。日期选取({
日期格式:“yyyy-mm-dd”,
多选:“100”
});
$(文档).ready(函数(){
$(“#按钮”)。单击(函数(){
var dates=$('#datepicker')。datepick('getDate');
控制台日志(日期);
*$('#datepicker1').val(日期)*
});
});

您无法直接获取日期数组,因为多选日期是通过默认分隔的逗号(,)传递的。因此,在提交表单数据之前,您需要进行一些小操作。下面是实现结果的自定义代码

jQuery代码

<input type="hidden" id="datepicker" name="dates[]" />
<script>
    $('#datepicker').datepick({

      dateFormat: 'yyyy-mm-dd',
      multiSelect: '100',

      //convert the selected date into array and assign the value to your hidden filed when the date picker will close by any format

      onClose: function(dates) { 
         var selectedDate = []; 
         for (var i = 0; i < dates.length; i++) { 
             selectedDate.push($.datepick.formatDate(dates[i])); 
         } 
         $('#datepicker').val(JSON.stringify(selectedDate));
      },

   });
</script>

希望它能像您预期的那样工作。

您是否获得了这个'var dates'变量的值?您使用的是哪个日期选择器库?如果您使用库,问题不是“如何在html输入中获取jquery数组”,而是“如何使用%lib\u NAME%在输入中添加数据”。告诉我们库名称或查看文档:)@SilentCoder不,我在印刷品中没有任何价值($Date);。这就是problem@RoryMcCrossan这一个。但我正在控制台中获取数组。