这是我代码的一部分,当用户单击按钮时,它会从数据库中检索与id有关的数据(即我没有获取net_id),我也会得到一个错误
TypeError: $(...).attr(...) is undefined
arr = $(this).attr('class').split( " " );
HTML
<td class="edit2 '.$rows["net_id"].' "> <input type="button" id="edit2" value="R" /> </td>
Ajax
<script type="text/javascript">
$(document).ready(function() {
$("#edit2").click(function() {
arr = $(this).attr('class').split( " " );
var clientid=document.getElementById("client").value;
$.ajax({ //create an ajax request to load_page.php
type: "POST",
url: "clientnetworkpricelist/display.php",
data: "clientid="+ clientid+"&rowid="+arr[1],
success: function(response){
$("#responsecontainer").html(response);
//alert(response);
}
});
});
});
</script>
Display.php
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxxxxx';
$dbPassword = 'xxxxxxxxxx';
$dbDatabase = 'fms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$client_id=$_POST['clientid'];
$rownum=$_POST['rowid'];
$sql="select status from $client_id WHERE net_id = ".$rownum."";
print $sql;
mysql_query($sql);
?>
您正试图获取$(this)的类,这是您单击的按钮。但该按钮没有类。它是您单击的要从中获取类的按钮的父级。
更改此行:
arr = $(this).attr('class').split( " " );
到此:
arr = $(this).parent().attr('class').split( " " );
您在以下行有更改:
arr = $(this).attr('class').split( " " );
至
arr = $(this).parent().attr('class').split( " " );
问题出在以下行:
arr = $(this).attr('class').split( " " );
您正在尝试获取被单击的按钮的class属性,因此它无法找到您需要的类。相反,你要做的是找到按钮的父级,即td
,并从中获取class属性
arr = $(this).parent().attr('class').split( " " );