更新:想通了...需要先在 DOM 中提升一个级别。
var map_id = $(this).parent().find(".map_id").attr('value');
var corrected_isin= $(this).parent().find(".corrected_isin").val();
var corrected_cusip = $(this).parent().find(".corrected_cusip").val();
var corrected_ticker = $(this).parent().find(".corrected_ticker").val();
我在让 jquery 为我工作时遇到了很多麻烦......我正在尝试更新数据表。我所有的更新代码都可以正常工作,但无论出于何种原因,我试图用 jquery 设置的变量都是未定义的。下面的代码没有设置变量。当我对变量进行硬编码时,它工作正常。我也尝试过使用 ID 而不是类,但无济于事。请帮忙!
谢谢你,大卫
var map_id = $(this).children(".map_id").attr('value');
var corrected_isin = $(this).children(".corrected_isin").val();
var corrected_cusip = $(this).children(".corrected_cusip").val();
var corrected_ticker = $(this).children(".corrected_ticker").val();
完整代码:
<html>
<head>
<title>ISIN Editor</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("form").submit(function() {
var map_id = $(this).children(".map_id").attr('value');
var corrected_isin = $(this).children(".corrected_isin").val();
var corrected_cusip = $(this).children(".corrected_cusip").val();
var corrected_ticker = $(this).children(".corrected_ticker").val();
$.ajax({
type: "GET",
url: "aj/table_edit_ajax.php",
data: {corrected_isin: corrected_isin, corrected_cusip: corrected_cusip, corrected_ticker: corrected_ticker, map_id: map_id},
cache: false,
success: function(){
alert(map_id + " " + corrected_isin + " " + corrected_cusip + " " + corrected_ticker );
}
});
return false;
});
});
</script>
</head>
<body>
<?php
include("aj/db.php");
$query = "SELECT map_id, ria_crd, position_cusip, position_ticker, position_description, position_first_date, position_last_date, map_corrected_isin, map_corrected_cusip, map_corrected_ticker FROM map_to_isin WHERE map_correct_status IN (2,5)";
$result = mysql_query($query,$bd);
?>
<table>
<tr>
<th>map_id</th>
<th>RIA CRD</th>
<th>Reported CUSIP</th>
<th>Reported Ticker</th>
<th>Reported Description</th>
<th>Date Range</th>
<th>New isin</th>
<th>New CUSIP</th>
<th>New Ticker</th>
<th>Submit Change</th>
</tr>
<?php
$row_num = 1;
while($row = mysql_fetch_array($result))
{
echo "<tr";
if($row_num%2) {
echo " bgcolor='pink'";
};
echo ">"; ?>
<form id="form<? echo $row['map_id'] ?>">
<input type="hidden" name="map_id" class="map_id" value="<? echo $row['map_id'] ?>"/>
<td><? echo $row['map_id'] ?></td>
<td><? echo $row['ria_crd'] ?></td>
<td><? echo $row['position_cusip'] ?></td>
<td><? echo $row['position_ticker'] ?></td>
<td><? echo $row['position_description'] ?></td>
<td><? echo $row['position_first_date'] . " -> " . $row['position_last_date'] ?></td>
<td><input type="text" name="corrected_isin" class="corrected_isin" value="<? echo $row['map_corrected_isin'] ?>"/></td>
<td><input type="text" name="corrected_cusip" class="corrected_cusip" value="<? echo $row['map_corrected_cusip'] ?>"/></td>
<td><input type="text" name="corrected_ticker" class="corrected_ticker" value="<? echo $row['map_corrected_ticker'] ?>"/></td>
<td><input type='submit' value='Update' /></td>
</form>
</tr>
<?php
$row_num ++;
}
echo "</table>";
mysql_close($bd);
?>
</body>
</html>