javascript - 在javascript中传递一个值

标签 javascript php jquery ajax

我在这里尝试 infotuts 的教程: http://www.infotuts.com/ajax-table-add-edit-delete-rows-dynamically-jquery-php/

还有一个像这样的 javascript:

 $(function(){
 $.ajax({
         url:"DbManipulate.php",
                  type:"POST",
                  data:"actionfunction=showData",
        cache: false,
        success: function(response){

          $('#demoajax').html(response);
          createInput();

        }

       });

现在我想添加一个参数,以便该行: url:"DbManipulate.php"变成 url:"DbManipulate.php?q=[some value]

我试着像这样修改脚本:

 var cat=2;

 $(function(){
 $.ajax({
         url:"DbManipulate.php?q="+cat.val(),
                  type:"POST",
                  data:"actionfunction=showData",
        cache: false,
        success: function(response){

          $('#demoajax').html(response);
          createInput();

        }

       });

但它不起作用。变量 cat 永远不会进入函数。如何传递变量“cat”以便 DbManipulate.php 文件接收 $q 变量并且我可以使用 $_GET 来使用它?

谢谢

最佳答案

尝试使用 GET 方法简单地以这种方式发送您的数据变量 (cat)

var cat=2;
$(function(){
 $.ajax({
         url:"DbManipulate.php",
         type:"GET",
         data:{actionfunction:showData,cat:cat},
         cache: false,
         success: function(response){
          console.log(response);
          $('#demoajax').html(response);
          createInput();
        }
       });

 // in DbManipulate.php, try to catch cat using $_GET like this
  $cat=$_GET['cat'];
 //do further processing

编辑

cat=2;
url="DbManipulate.php";
function yourFunc(cat,url){
$.ajax({
    type: "GET",
    url: url+'?q='+cat,
    dataType: "json",
    cache: false,
    success: function (response) {
        $('#demoajax').html(response);
              createInput();
    }
});
}
//in DbManipulate.php 
 $cat=$_GET['q'];

更多信息:http://api.jquery.com/jquery.ajax/

关于javascript - 在javascript中传递一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28259422/

相关文章:

javascript - 谷歌图表重绘/缩放窗口调整大小

javascript - 选择下一个(不是立即)输入标签

javascript - javascript中 "click"事件的嵌套函数调用

php - 如何在php中取消链接图像

php - 插入和选择嵌套查询中的 SQL 语法错误

javascript - 如何创建 "narrow by"过滤菜单?

javascript - 使用 vueJs 而不是 jquery

Javascript 类调用 Super 并在回调中使用它

javascript - react : Add jquery needed by another library

php - 将第三方 Web 内容导入我的站点以便通过 SSL 发送的安全方法?