简体   繁体   English

JavaScript无法将值传递给函数

[英]Javascript Not Passing Value to Function

Hi I am trying to pass 3 values into some Javascript. 嗨,我正在尝试将3个值传递给某些Javascript。 This works for the first function. 这适用于第一个功能。 All values are found to be correct here...... 在这里发现所有值都是正确的……

function handleClick(cb,colum,id) {
  if (cb.checked == true){
     var checked = 1;
  } else {
     var checked = 0;
  }
  sendHddToPHP2(checked,column,id);
}

But once the second function is called I get nothing.... 但是一旦调用第二个函数,我什么也没得到。

function sendHddToPHP2(editableObj,column,id) {
  window.alert(editableObj);
  $.ajax({
          url: "update/hdd.php",
          type: "POST",
          data:'column='+column+'&editval='+editableObj+'&id='+id,
          success: function(data){
                   $(editableObj).css("background","#FDFDFD");
                   }
  });
}

I added the alert to check if the variable was present and it didn't run. 我添加了警报,以检查变量是否存在并且没有运行。 I can't see any typos and my knowledge isn't that great on Javascript. 我看不到任何错字,而且我对Javascript的了解也不是很好。 Is there something I have missed? 有什么我想念的吗?

You have a typo: "colum" in the argument list. 您有一个错字:参数列表中的“ colum”。

Also, seems like you want to access the check box later in the Ajax callback, that will fail since you're trying to reference it via the "editableObj" value of the checkbox rather than the element. 另外,似乎您想稍后再访问Ajax回调中的复选框,这将失败,因为您试图通过复选框的“ editableObj”值而不是元素来引用它。

As I understand you want your function to be like this: 据我了解,您希望您的功能像这样:

function handleClick(cb,colum,id) {
  var checked = (cb.checked)?1:0;//same as(cb.checked==true)1:0
  sendHddToPHP2(checked,column,id);
}

but are you sure you want to return a 1 or 0? 但是您确定要返回1或0? because in the second function you will be using $(1).css("background","#FDFDFD"); $(0).css("background","#FDFDFD"); 因为在第二个函数中,您将使用$(1).css("background","#FDFDFD"); $(0).css("background","#FDFDFD"); $(1).css("background","#FDFDFD"); $(0).css("background","#FDFDFD");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM