我通过get联系了另一个网站,并得到了一个xml的结果来解释。
我是这样联系的:
$v= file_get_contents(
"https://www.v.com/fechasexcursion.php?
agent=M&password=s&fecha1=2018-10-05&fecha2=2018-12-
31&idmodelo=$cifraexcursion");
然后,我解析信息以从xml中获取日期,从而在datepicker中禁用这些日期:
$cifraexcursion = 9;
$xml = new SimpleXMLElement($v);
foreach ($xml->excursion as $tour) {
if ($nameoftour->idmodelo == $cifraexcursion) {
echo "<br>Date ".$nameoftour->date;
然后,我需要将这些日期添加到一个javascript var中,以便在datepicker中使用。下面可以很好地工作,但我不能处理数组$nameoftour->date
<script>
var array = ["2018-10-14","2018-10-15","2018-10-16"];
$('input').datepicker({
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [ array.indexOf(string) == -1 ]
}
});
(function() {
$.datepicker.setDefaults({
regional: 'en',
buttonImage: "/images/calendar_blue.png",
buttonImageOnly: true,
firstDay: 1,
})
})();
</script>
更新:打印出4个日期:
echo "<br>Date ".$nameoftour->date;
但是,如果我这样做,试图保存为javascript数组,它可以工作,但我只能得到四个日期中的最后一个日期。
var array=["<?php echo ($nameoftour->date);?>",];
更新:我让它工作了:
$xml = new SimpleXMLElement($viajes);
$tmpArrayFechas = [];
foreach ($xml->excursion as $excursion) {
if ($excursion->idmodelo == $cifraexcursion) {
$tmpArrayFechas[] = $excursion->fecha;
}
}
$tmpArrayFechas = implode(',', $tmpArrayFechas);
?>
<input type="hidden" id="plazasDisponibles"
name="plazasDisponibles"
value="<?php echo $tmpArrayFechas; ?>">
<script>
var arrayDiasDisponibles =
document.querySelector("#plazasDisponibles").value.split(',');
$('input').datepicker({
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [ arrayDiasDisponibles.indexOf(string) !== -1 ]
}
});
(function() {
$.datepicker.setDefaults({
regional: 'en',
buttonImage: "/images/calendar_blue.png",
buttonImageOnly: true,
firstDay: 1,
})
})();
</script>
发布于 2018-10-06 02:18:51
既然您似乎在使用jQuery,那么您可以使用$.parseXML,然后您可以将其包装在jQuery和.find('date')
中。类似这样的代码应该可以:
var output = Array.from( // if you need an array (and not just iterable object)
$( // wrap the XMLDocument in jQuery to parse it with .find
$.parseXML(rawXmlData) // parse the XML into an XMLDocument
)
.find('date') // find the nodes we need
.map(function (index, element) { // map the tags/nodes
return $(element).text(); // retrieve only the content
})
);
发布于 2018-10-06 02:50:50
以下是一些代码将xml解析为数组的原始js示例……
var myXml = "<root><tour><date>2018-10-14</date></tour><tour><date>2018-10-15</date></tour><tour><date>2018-10-18</date></tour></root>";
if (window.DOMParser)
{
parser = new DOMParser();
console.log(parser);
xmlDoc = parser.parseFromString(myXml, "text/xml");
}
else // Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(myXml);
}
var elementsCollection = xmlDoc.getElementsByTagName("date");
var myArray = [];
for(var i = 0; i < elementsCollection.length; i++) {
myArray.push(elementsCollection[i].innerHTML);
};
for(i = 0; i < myArray.length; i++)
{
console.log(myArray[i]);
document.write(myArray[i] + "<br/>");
}
我发现下面这篇文章很有帮助:Parse XML using JavaScript
还要注意,如果您没有一个根元素,dom解析器将不能很好地解析您的xml,因此您会注意到,在我的示例中,我添加了一个元素。
https://stackoverflow.com/questions/52669861
复制相似问题