【问题标题】:Hi, Can someone help me understand the javascript syntax, especially in data: parts嗨,有人可以帮我理解 javascript 语法,尤其是在 data:parts 中
【发布时间】:2017-05-03 08:34:32
【问题描述】:

我正在从 Internet 下载日历 html/php 文档,并想了解它是如何工作的。有人可以帮助我理解这个 java 脚本的作用吗? 同一个页面已经有getCalender函数了。

<script type="text/javascript">
    function getCalendar(target_div,year,month){
        $.ajax({
            type:'POST',
            url:'functions.php',
            data:'func=getCalender&year='+year+'&month='+month,
            success:function(html){
                $('#'+target_div).html(html);
            }
        });
    }

谢谢

【问题讨论】:

标签: javascript php syntax


【解决方案1】:

您帖子中的 Javascript 使用 jquerys ajax 函数向后端 function.php 文件发出请求。通过的参数是年和月。查看success 回调,function.php 文件正在构建并返回 Html 以显示日历。

Ajax 用于异步加载内容,因此您无需刷新页面。

这里是关于 jquery ajax 功能的更多信息

http://api.jquery.com/jquery.ajax/

正在发送的数据

因此,查看发送到function.php 文件的键值字段,这些是POSTed 数据。所以你可以使用PHP 超级全局变量$_POST 来访问这些。

例如:

<?php
$_POST['func'] = 'getCalender';
$_POST['year'] = 2017 //assuming the value stored in the year variable
$_POST['month'] = 'May //assuming the value stored in the month variable

在上面的示例中,看起来好像我正在将值分配给超变量,即 Iam,但我想要做的是向您展示它们可能相等的示例以及如何访问数据

【讨论】:

  • 感谢您的链接。您能否更深入地了解“data:'func=getCalender&year='+year+'&month='+month”部分? (不能在链接中提供此信息)传递参数的语法是什么?是“+”号还是“&”号。谢谢
  • @Jo ha + 只是将字符串连接到变量。 &amp; 是表示一个键值对何时结束而下一个键值对开始的分隔符。
猜你喜欢
  • 2021-09-18
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多