2014-10-11 114 views
0

我创建一个页面,应该从数据库中读取数据,将其显示为表格,并允许按行进行编辑(使用AJAX调用)。下面是简要的代码段,其中的问题就来了 -JAVASCRIPT:通过的参数是“未定义”

的Javascript:

function editParam(bname, button) { 
alert(bname); //this part doesn't happen, the error on console comes up before it 
tr = button.parentNode; 
while (tr && tr.nodeName.toUpperCase() !== "TR" && tr.nodeName.toUpperCase() !== "BODY") { 
    tr = tr.parentNode; 
} 
if (!tr || tr.nodeName.toUpperCase() !== "TR") { 
    return; 
} 
//some more code 
} 

PHP:

//get $row_users['Name'] 
while ($row_users = mysqli_fetch_array($results)) { 
echo "<form method='POST'> //some <tr><td></td></tr> 
<button onclick='editParam(".$row_users['Name'].", this)'>Edit</button> 
</form>"; 
} 

我在列名称的数据就像是 “B1”, “C2”,等等。

错误onclick按钮:B1未定义。

运行其他文本或数字的代码时,它工作正常。所以我明白我的问题与数据类型有关。但是,我不知道如何解决相同的问题。

任何建议将不胜感激,谢谢!

+1

您需要将一个字符串传递给函数。 – CBroe 2014-10-11 13:20:12

+0

@CBroe你能解释一下这种情况吗? – VGupta 2014-10-11 13:26:48

+0

您正在抛出名称,所以得到的属性值看起来像'editParam(B1,this)' - 应该是'editParam(“B1”,this)' – Pointy 2014-10-11 13:27:21

回答

1

您需要在$row_users['Name']附近添加引号,否则它会被解释为JS变量。

"<button onclick='editParam(\"" . $row_users['Name'] . "\", this)'>Edit</button>"; 
+0

Aah它的工作!万分感谢! – VGupta 2014-10-11 13:34:44