[英]Submitting form issues: TypeError: this.form is null
我有一个php页面,该页面在表格中显示给定站点的所有服务日志。
在表的最后,我有一个新行,其中有一个“添加新服务日志”按钮。
单击该按钮可隐藏该行,并显示具有表单和所需输入的新行以及具有“保存”和“取消”按钮的新行。
“保存”按钮调用Javascript函数,该函数检查键盘输入是否已填写。 当点击“保存”时,我会在控制台日志中看到
TypeError:this.form为null
这是我的代码:
// SQL Query
// Table data of queried results
// Add a new service log
echo "<tr id='AddNew'>
<td><input type=button value='Add New' onclick=\"$('#AddNew').hide();$('#AddNewHid').show();$('#SaveCancel').show();\"></td>
<td colspan=12>   </td></tr>
<form name='AddNewLog' method='POST' action='servicelook.php' id='AddNewService'>
<tr style='display: none;' id='AddNewHid'>
<td><input type='text' value='$lastID' name='ticketid' size=10></td>
<td><input type='text' value='$siteID' name='SiteID' size=4></td>
<td><input type='text' value='$siteName' name='SiteName' size=20></td>
<td><input type='text' value='$userid' name='takenBy' size=4></td>
<td><input type='text' value='$now' name='callTime' size=20></td>
<td><input type='text' value='' name='caller' size=20></td>
<td><input type='text' value='' name='callPhone' size=14></td>
<td><textarea name='issue' value = '' rows=3 cols=10></textarea></td>
<td><textarea name='fix' value = '' rows=3 cols=10></textarea></td>
<td><input type='text' value='$userid' name='solvedBy' size=4></td>
<td><input type='text' value='$now' name='solvedTime' size=20></td>
<td style='min-width:100px;max-width:100px;'><input type='checkbox' name='fup' value='fup'>Follow Up</td></tr>
<input type='hidden' value='Yes' name='AddNew'>
<input type='hidden' value='$userid' name='userid'>
<input type='hidden' value='$session' name='session'>
<input type='hidden' value='$siteid' name='siteid'>
<tr style='display: none;' id='SaveCancel'>
<td colspan=2>
<input name='save' type='button' onclick='inputCheck(this.form.issue);' value='Save'>  
<input type='button' value='Cancel' onclick=\"$('#AddNew').show();$('#AddNewHid').hide();$('#SaveCancel').hide();\"></td>
<td colspan=11>   </td>
</tr></form>";
echo "</table>";
我的inputCheck函数如下所示:
<script type="text/javascript">
function inputCheck(areaName)
{
console.log("checking...");
if (areaName.value.length > 0)
{
$('form#AddNewService').submit();
}
else
{
alert("You didn't describe the problem!");
}
} // end noteCheck function
</script>
我尝试将提交按钮更改为此:
<input type=submit value=Save>
但没有任何反应,页面或日志中没有任何内容。
有人对我在做什么错或这里存在什么问题有任何想法吗?
提前致谢!
编辑:
根据史蒂文的建议,我能够成功提交表格。 但是,输入未正确发送。 我正在提交到同一页面,以便重新加载表,并且用户可以看到其最新条目。 在脚本的开头,我有以下内容:
if($AddNew == 'Yes')
{
echo "YES...Adding New";
echo $ticketid .$SiteID. $SiteName. $takenBy. $callTime. $caller. $callPhone. $issue. $fix. $solvedBy. $solvedTime;
// Start SQL Query
}
输入返回空值。 我设置输入的方式有问题吗?
这里的一般策略是使保存按钮成为提交按钮,然后将验证置于表单的提交处理程序中,如果验证失败,则取消提交,例如
<form onsubmit="return inputCheck(this.issue);" ...>
然后,如果验证失败,则从inputCheck函数返回false
。
您的问题可能是因为您有一个无法放置在某个地方的表格,所以该表格已被移到桌子外,即您有:
<table>
<form>
...
</form>
</table>
浏览器正在“纠正”为:
<table>
...
</table>
<form>
</form>
现在,表格在表格外部,但控件仍在表格内部。 因此,将标记更改为:
<form>
<table>
...
</table>
</form>
因此表格和表单控件保留在表单内。
当this.form为null时:
onclick='inputCheck(this.form.issue);'
用。。。来代替:
onclick='inputCheck();'
并编辑您的inputCheck方法:
function inputCheck() {
console.log("checking...");
if ($('textarea[name=issue]').val().length > 0){
$('form#AddNewService').submit();
} else {
alert("You didn't describe the problem!");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.