如何将 php 数组转换为 javascript 数组以与 Jquery UI 日期选择器一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21672267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert php array into javascript array to use with the Jquery UI datepicker?
提问by chap
Right now I am using a manually inputted array of dates called disable to disable dates in the jquery UI datepicker with the beforeShowDay method. This is working fine, however, I have a php variable $disablethese which is storing a dynamic array of dates to disable. For some reason, I can't seem to convert my php array into a javascript array (which I'm calling unavailabledates). It doesn't throw any errors but it just doesn't work block out the dates the same way as the static array.
现在,我正在使用一个名为 disable 的手动输入的日期数组,通过 beforeShowDay 方法禁用 jquery UI 日期选择器中的日期。这工作正常,但是,我有一个 php 变量 $disablethese,它存储要禁用的动态日期数组。出于某种原因,我似乎无法将我的 php 数组转换为 javascript 数组(我称之为不可用日期)。它不会抛出任何错误,但它不会像静态数组一样阻止日期。
<script type="text/javascript">
var unavailabledates = <?php echo json_encode($disablethese); ?>;
</script>
<script src="js/datepicker-admin.js"></script>
Here is datepicker-admin.js
这是 datepicker-admin.js
$(document).ready(function() {
var disable = ["2014-01-03","2014-01-13","2014-01-23"];
$('#fromDate').datepicker({
beforeShowDay: function(date) {
if($.inArray($.datepicker.formatDate('yy-mm-dd', date ), disable) > -1) {
return [false, "highlighted", "Booked out"];
} else {
return [true, "", "available"];
}
}
});
});
回答by anurupr
you can use $.parseJSON
function.
你可以使用$.parseJSON
函数。
<script type="text/javascript">
var unavailabledates = $.parseJSON('<?php echo json_encode($disablethese); ?>');
</script>
<script src="js/datepicker-admin.js"></script>
回答by Realdheeraj
You can use directly JSON.parse()
你可以直接使用 JSON.parse()
var unavailabledates = JSON.parse(<?php echo json_encode($disablethese); ?>);
回答by Shaunak Shukla
You can use .push
to insert all values to an array.
您可以使用.push
将所有值插入到数组中。
<script type="text/javascript">
var unavailabledates = new Array();
<?php
$disbaleddates = json_encode($disablethese);
for($i=0;$i<count($disbaleddates);$i++) { ?>;
unavailabledates.push('<?=$disbaleddates[$i]?>');
<?php } ?>
</script>
<script src="js/datepicker-admin.js"></script>
This may solve your problem, But it's might have a long way!! :)
这可能会解决您的问题,但可能还有很长的路要走!!:)
回答by Jain
1) Create a hidden field in php page:<input id="disable-dates" type="hidden" value="<?php echo json_encode($disablethese); ?>">
1)在php页面中创建一个隐藏字段:<input id="disable-dates" type="hidden" value="<?php echo json_encode($disablethese); ?>">
2) Use jquery :
2)使用jQuery:
<script type="text/javascript">
var unavailabledates = $.parseJSON($('#disable-dates').val());
</script>
Both simulatneously
两者同时
回答by Fahim
Did you assign your unavailabledates in
您是否指定了您的不可用日期
var disable = ["2014-01-03","2014-01-13","2014-01-23"];
var disable = ["2014-01-03","2014-01-13","2014-01-23"];
like
喜欢
var disable = unavailabledates;
var 禁用 = 不可用日期;
回答by Bradley Kovacs
I'm using laravel and this works beautifully:
我正在使用 laravel,这很好用:
var something = JSON.parse('{!! json_encode(['foo' => 'bar']) !!}');
I was very confused for a little bit cause I couldn't figure out why this DOES NOT work:
我有点困惑,因为我不明白为什么这不起作用:
var something = JSON.parse('{{ json_encode(['foo' => 'bar']) }}');
the second way escapes HTML entities in the document. but yea, that's only with laravel.
第二种方法转义文档中的 HTML 实体。但是,是的,这仅适用于 laravel。
回答by Hemanth
var obj = <?php echo json_encode($_GET) ?>;