嗨,我正在写一段代码来获取文本框的值。尽管所有其他值都打印得很好,但alert(FormID)打印为Undefined。在这里,我已经清楚地评论了我所得到的。请有人帮我展示一下我所犯的错误。
<html>
<title></title>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script></head>
<script src="UserTypeSubmit.js"></script></head>
<script>
$(document).ready(function(){
$(".FormEdit").click(function(){
var B=$(this).attr('id');// Ex: Getting ID as FormEdit1 if click on first Edit link
var ReplacesVal=B.replace("FormEdit","");//Prints Value as 1 a I I need.
alert("FormIDInput"+ReplacesVal);//Alert : FormIDInput1
var FormID=$("#FormIDInput"+ReplacesVal).val();
alert(FormID);// Alert: Undefine. Im expecting the value 1 to be in the alert as $("#FormIDInput"+ReplacesVal).val() is equal to 1. and it prints well in the text box.
});
return false;
});
</script>
</head>
<body>
<div class="User" style="color:#0B173B">Forms_Available_to_Collect_Pubic_Health_Information </div>
<br><br>
<?php
include 'connectionPHP.php';
$result=mysqli_query($con,"SELECT * FROM Form");
echo "<table border='1' id='DisplayFormDetailss'>
<tr style='background-color:#D0A9F5;' height='40px;'>
<th width='100px;'>Form ID</th>
<th width='420px;'>Form Name</th>
<th width='100px;'>Inactivate</th>
<th width='70px;'>Edit</th>
<th width='70px;'>Delete</th>
<th width='70px;'>Save</th>
</tr>";
$i=1;
while($row = mysqli_fetch_array($result))
{
$w=$row['Form_ID'];
$v=$row['Form_Name'];
echo "<tr height='25px;'>";
echo "<input id='FormIDInput ".$i."' value='$w' ></input>";
//Prints 1,2,3,4,5,6 as the values of input boxes perfectly.
echo "<td name='FormID[]' class='FormID' align='center' value='$w'>$w</td>";
echo "<td name='FormName[]' id='FormName".$i."' align='left'>$row[Form_Name]</td>";
echo "<td name='FormInactive[]' id='FormInactive".$i."' align='center'><input type='checkbox'></input></td>";
echo "<td class='FormEdit' id='FormEdit".$i."' align='center'><a href='' align=left>Edit</a></td>";
echo "<td name='FormDelete[]' id='FormDelete".$i."' align='center'><a href='' align=left>Delete</td>";
echo "<td name='FormSave[]' id='FormSave".$i."' align='center'><a href='' align=left>Save</td>";
echo "</tr>";
$i++;
}
echo "</table>";
?>
</body>
</html>
在PHP代码中,输入框中有一个空格:
echo "<input id='FormIDInput ".$i."' value='$w' ></input>";
试着用代替它
echo "<input id='FormIDInput".$i."' value='$w' ></input>";
在输入类型字段中缺少type='text'&太空问题:
echo "<input type='text' id='FormIDInput".$i."' value='$w' ></input>";
您需要使用id='FormIDInput".$i."'
而不是id='FormIDInput ".$i."'
。您可以使用FormIDInput
的FormID[]
输入文本文件
尝试使用,
echo "<td name='FormID[]' id='FormIDInput".$i."' class='FormID' align='center' value='$w'>$w</td>";
而不是
echo "<input id='FormIDInput ".$i."' value='$w' ></input>";
//Prints 1,2,3,4,5,6 as the values of input boxes perfectly.
echo "<td name='FormID[]' class='FormID' align='center' value='$w'>$w</td>";