Alright so I am trying to basically take what's in $row['cardid'] and set it into the value of my div tag. Then on the html file this is echoing to, it would run my function which uses that value from the div tag. Right now when I use the onclick, it pulls up but says my value is undefined. So my question is why is my variable undefined when I pull it up using html?
好吧所以我试图基本上采取$ row ['cardid']中的内容并将其设置为我的div标签的值。然后在回显的html文件中,它将运行我的函数,该函数使用div标签中的该值。现在,当我使用onclick时,它会拉起但是我的值是未定义的。所以我的问题是,当我使用html将其提取时,为什么我的变量未定义?
<?php
$q=$_GET["q"];
$con = mysql_connect("*", "*", "*");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*", $con);
$sql="SELECT cardset, cardname, cardnumber, cardid FROM cards WHERE cardname LIKE '%".$q."%' OR cardset LIKE '%".$q."%'";
$result = mysql_query($sql) or die ('Error: '.mysql_error ());
echo "<table border='1'>
<tr>
<th>#</th>
<th>Cardname</th>
<th>Card Set</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['cardnumber'] . "</td>";
// This is the troubled line.
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage(this.value)">' . $row['cardname'] . '</div></td>';
echo "<td>" . $row['cardid'] . "</td>";
echo "<td>" . $row['cardset'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
4 个解决方案
#1
1
I don't fully understand your question, but you mean just something like this?
我不完全理解你的问题,但你的意思是这样的?
echo '<td><div onclick="changeimage(this.id)" id="'.$row['cardid'].'">' . $row['cardname'] . '</div></td>';
Use ID instead of value basically..
基本上使用ID而不是值..
Example: jsFiddle
示例:jsFiddle
#2
1
div
elements doesn't have a value
property, but you can get the value of the attribute
named value
- you should use .getAttribute("value")
:
div元素没有value属性,但是你可以获得名为value的属性的值 - 你应该使用.getAttribute(“value”):
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage(this.getAttribute(\"value\")">' . $row['cardname'] . '</div></td>';
An example: http://jsfiddle.net/vaSYh/
一个例子:http://jsfiddle.net/vaSYh/
Also, if you need to store data in DOM elements, use data-
attributes. (Or just name them that way even if you don't have to support HTML5 right now; It will make the transition easier)
此外,如果需要在DOM元素中存储数据,请使用数据属性。 (或者即使您现在不必支持HTML5,也可以这样命名;它将使转换更容易)
#3
1
Don’t use this.value. It’s not a form.
不要使用this.value。这不是一种形式。
Use this.innerText or this.innerHtml to get the content of the div
使用this.innerText或this.innerHtml获取div的内容
#4
0
If I understand correctly your answer, you want to alert the value of $row['cardid']? With your code, you can't get the correct value, because you're mixing up javascript code with php code. The 'this' keyword in your 'onclick' event handler, is the javascript function itself, then it has not a 'value' property. Try this code, to alert the right value:
如果我理解你的答案,你想提醒$ row ['cardid']的价值?使用您的代码,您无法获得正确的值,因为您将javascript代码与PHP代码混合在一起。 'onclick'事件处理程序中的'this'关键字是javascript函数本身,然后它没有'value'属性。尝试此代码,以提醒正确的值:
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage('.$row['cardid'].'">'. $row['cardname'] . '</div></td>';
#1
1
I don't fully understand your question, but you mean just something like this?
我不完全理解你的问题,但你的意思是这样的?
echo '<td><div onclick="changeimage(this.id)" id="'.$row['cardid'].'">' . $row['cardname'] . '</div></td>';
Use ID instead of value basically..
基本上使用ID而不是值..
Example: jsFiddle
示例:jsFiddle
#2
1
div
elements doesn't have a value
property, but you can get the value of the attribute
named value
- you should use .getAttribute("value")
:
div元素没有value属性,但是你可以获得名为value的属性的值 - 你应该使用.getAttribute(“value”):
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage(this.getAttribute(\"value\")">' . $row['cardname'] . '</div></td>';
An example: http://jsfiddle.net/vaSYh/
一个例子:http://jsfiddle.net/vaSYh/
Also, if you need to store data in DOM elements, use data-
attributes. (Or just name them that way even if you don't have to support HTML5 right now; It will make the transition easier)
此外,如果需要在DOM元素中存储数据,请使用数据属性。 (或者即使您现在不必支持HTML5,也可以这样命名;它将使转换更容易)
#3
1
Don’t use this.value. It’s not a form.
不要使用this.value。这不是一种形式。
Use this.innerText or this.innerHtml to get the content of the div
使用this.innerText或this.innerHtml获取div的内容
#4
0
If I understand correctly your answer, you want to alert the value of $row['cardid']? With your code, you can't get the correct value, because you're mixing up javascript code with php code. The 'this' keyword in your 'onclick' event handler, is the javascript function itself, then it has not a 'value' property. Try this code, to alert the right value:
如果我理解你的答案,你想提醒$ row ['cardid']的价值?使用您的代码,您无法获得正确的值,因为您将javascript代码与PHP代码混合在一起。 'onclick'事件处理程序中的'this'关键字是javascript函数本身,然后它没有'value'属性。尝试此代码,以提醒正确的值:
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage('.$row['cardid'].'">'. $row['cardname'] . '</div></td>';