继昨天的问题之后,我现在让下面的代码成功运作了。它允许我更改第一个表单项并在后台将其提交到“process.php”并将字段变为绿色。但是,触发器仅适用于第一个表单项,在本例中为“cstate”。当“clocation”改变时它不会触发。如果您更改clocation然后cstate然后两个表单都提交正常,那么只是在更改clocation时不会触发.change函数。我对JS(总JS noob)不够好,知道它为什么不起作用所以我很感激你能给我的任何帮助。
谢谢!
$sql = "select * from `$table1`";
$result = mysql_query ($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$carid = $row["car_id"];
$carnum = $row["carnum"];
$carlocation = $row["carlocation"];
$carstate = $row["carstate"];
$formname = "#form".$carid;
print '<script type="text/javascript">';
print " var cnum;";
print " cnum = '$formname',";
print "
$('form').change(function()
{
console.log(cnum);
$.ajax({
type: 'post',
url: 'process.php',
data: $(this).serialize(),
success: function() {
}
});
return false;
});
</script>";
echo "<table>";
echo "<tr id='$carid'>";
echo "<td>$carnum</td>";
echo "<td><form action='' method='post' id='form$carid'>";
echo "<select id='popup' name='cstate'>";
echo "<option value='In-Service-Bay'>In Service Bay</option>";
echo "<option value='Awaiting-Service'>Awaiting Service</option>";
echo "<option value='Service-Complete'>Service Complete</option>";
echo "</select></td>";
echo "<select id='popup' name='clocation'>";
echo "<option value='Carpark-1'>Carpark-1</option>";
echo "<option value='Carpark-2'>Carpark-2</option>";
echo "<option value='Carpark-3'>Carpark-3</option>";
echo "</select></td>";
echo "</form></tr>";
}
echo "</table>";
答案 0 :(得分:0)
使用.on()
并将代码包装在document.ready.
$(document).ready(function(){
$('form').on('submit',function()
{
//code here
});
});
也将此代码放在while
循环之外。使用.submit()
答案 1 :(得分:0)
通过为每个输入选择使用单独的表单解决它。现在工作非常出色。
感谢所有那些试图提供帮助的人。
亚当