简体   繁体   English

Javascript / DOM为什么我的表单不支持Submit()?

[英]Javascript/DOM Why is does my form not support submit()?

This is my first time working with asp.net and javascript, so I don't know a lot of the nice web resources, or much about debugging javascript. 这是我第一次使用asp.net和javascript,因此我不了解很多不错的网络资源,也不太了解调试javascript。 I'm looking to find out why on line oFormObject.submit(); 我正在寻找为什么在oFormObject.submit(); Microsoft JScript runtime error: Object doesn't support this property or method. Microsoft JScript运行时错误:对象不支持此属性或方法。

I'm using links to function as buttons because I'm terrible at aesthetics and I think links will look more professional than a table of 50 buttons. 我使用链接来充当按钮,是因为我在美学方面很糟糕,而且我认为链接看起来比50个按钮的表更为专业。 I read that this solution may cause issues with browsers trying to pre-render my links and causing lots of extra traffic and problems, but the Css I found to make a button look like a link seemed to have alignment and spacing issues. 我读到,这种解决方案可能会导致浏览器尝试预渲染我的链接并导致大量额外的流量和问题,但是我发现使按钮看起来像链接的CSS似乎存在对齐和间距问题。

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><link rel="stylesheet" type="text/css" href="DefectSeverity.css" /><title>
  Defect Severity Assessment Code List
</title></head>
<body>
<form name="form1" method="post" action="CodeList.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNjQzMTI3NjU4ZGSW2wNBW3eGztyO+Tftc5BB8A6cMg==" />
</div>

<div>
<p>Welcome Maslow</p><SCRIPT language="JavaScript">
function submitForm(intId)
{ oFormObject= document.getElementById(""form"+_StrCodeId + @""");
oFormElement= document.getElementById("codeId");
oFormElement.value=intId;
  oFormObject.submit();
}
</SCRIPT> <form method="post" action="CodeAssessment.aspx" id="formcodeId">
<table name="codeId" id="codeId" value="0" type="hidden" class="linkTable">
<tr>
   <th>Completed</th><th>Code</th><th>Description</th><th>Code Present Since</th>
</tr><tr class="row">
<td><input name="401" type="checkbox" value="401" DISABLED /></td><td><a href="javascript:submitForm(0);">401</a></td><td>Missing Original Document/form</td><td>2009.10.16</td>
</tr><tr class="rowAlternate">
<td><input name="NDMI" type="checkbox" checked="checked" value="NDMI" DISABLED /></td>
<td><a href="javascript:submitForm(1);">NDMI</a></td>
<td>Note date is missing</td>    <td>2009.10.15</td>
</tr>
 </table><input type="submit" />
</form>
</div>
</form>
</body>
</html>

If I change the script line to oFormObject= document.forms[0]; 如果我将脚本行更改为oFormObject= document.forms[0]; it submits the asp.net's viewstate form posting back to the same page instead of where I want to go. 它提交了asp.net的viewstate表单,该表单回发到同一页面而不是我要去的地方。 so the rest of the code on the page appears to work. 因此页面上的其余代码似乎可以正常工作。

I'm not really into asp.net but into javascript, so I'd say there's a parse error in 我不是真的喜欢asp.net,而是喜欢javascript,所以我说在其中存在解析错误

oFormObject= document.getElementById(""form"+_StrCodeId + @""");

the quotes just don't match. 引号不匹配。 i'd say you try to change it to 我会说你尝试将其更改为

oFormObject= document.getElementById("formcodeId");

instead 代替

Ok apparently you can't nest forms in html/DOM. 好的,显然您不能将表单嵌套在html / DOM中。 I have changed my asp.net provided form and now it navigates but crashes saying 我已经更改了asp.net提供的表单,现在可以导航但崩溃了

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><link rel="stylesheet" type="text/css" href="DefectSeverity.css" /><title>
 Defect Severity Assessment Code List
</title></head>
<body>
<form name="form1" method="post" action="CodeAssessment.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNjQzMTI3NjU4ZGSW2wNBW3eGztyO+Tftc5BB8A6cMg==" />
</div>

<div>
<p>Welcome Maslow</p><SCRIPT language="JavaScript">
function submitForm(intId)
{ oFormObject= document.forms[0];
oFormElement= document.getElementById("codeId");
oFormElement.value=intId;
oFormObject.submit();
}

</SCRIPT> <table name="codeId" id="codeId" value="0" type="hidden" class="linkTable">
<tr>
    <th>Completed</th><th>Code</th><th>Description</th><th>Code Present Since</th>
</tr><tr class="row">
    <td><input name="401" type="checkbox" value="401" DISABLED /></td>
<td><a href="javascript:submitForm(0);">401</a></td><td>Missing Original Document/form</td><td>2009.10.16</td>

</tr><tr class="rowAlternate">
    <td><input name="NDMI" type="checkbox" checked="checked" value="NDMI" DISABLED /></td>
<td><a href="javascript:submitForm(1);">NDMI</a></td><td>Note date is missing</td><td>2009.10.15</td>
</tr>
</table><input id="testSubmit" type="submit" />
</div>
</form>
</body>
</html>

I don't see what the issue is now, the code you posted in your answer worked fine for me, only problem I have is that I dont have a CodeAssesment.aspx page so I get a HTML404 error, but thats it. 我看不出现在是什么问题,您在答案中发布的代码对我来说效果很好,唯一的问题是我没有CodeAssesment.aspx页面,所以我收到了HTML404错误,仅此而已。 Your original code document.getElementById(""form"+_StrCodeId + @"""); 您的原始代码document.getElementById(“” form“ + _ St​​rCodeId + @”“”); was not valid, and you don't have a variable called StrCodeId, 是无效的,并且您没有名为StrCodeId的变量,

Try 尝试

oFormObject = document.getElementById("form"+intId);

Ok solved the crashing, so I put another answer, its because you've copied the source code and used that to create the outline again, I think, as you shouldn't have the hidden input viewstate as part of the page, it gets generated automatically on build. 好了,解决了当机问题,所以我提出了另一个答案,因为您已经复制了源代码,并使用它再次创建了大纲,我想,因为您不应该将隐藏的输入viewstate作为页面的一部分,所以它可以在生成时自动生成。 Remove this line and try it 删除此行并尝试

<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNjQzMTI3NjU4ZGSW2wNBW3eGztyO+Tftc5BB8A6cMg==" />
</div>

I trimmed out my dynamic code generation section 我整理了动态代码生成部分

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html 
 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head><link rel="stylesheet" type="text/css" href="DefectSeverity.css" /><title>
 Defect Severity Assessment Code List
</title><script type="text/javascript">
function submitForm(intId)
{ oFormObject= document.forms[0];
oFormElement= document.getElementById("codeId");
oFormElement.value=intId;
  oFormObject.submit();
}
</script> </head>
<body>
<form name="form1" method="post" action="CodeAssessment.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJMzMxMzk5NjY5ZGTQg8pLRPHaRM4Idd6LyKDmFvMpNA==" />
</div>

<div>
<input type="submit" />
</div>
</form>
</body>
</html>

Hey look there's another div again. 嘿,看起来又有一个div了。 I don't see where this is coming from. 我不知道这是哪里来的。 and I'm still getting Validation of viewstate MAC failed. 而且我仍然收到viewstate MAC的验证失败。 If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. 如果此应用程序由Web场或群集托管,请确保配置指定相同的validationKey和验证算法。 AutoGenerate cannot be used in a cluster. 自动生成不能在群集中使用。

So we solved this in another answer, but using comments, so this is just incase anyone else has the same problem, 因此,我们在另一个答案中解决了这个问题,但是使用了注释,以防万一其他人遇到相同的问题,

It's all down to the form having a runat="server" attribute, this generates the viewstate input, then when the form is submitted to the second page, it tries to fill out the page with the details from the viewstate, however as in this case the form is submitted to another page, the two pages don't match up and when it tries to deal with the viewstate input it throws the error. 一切都归结到具有runat =“ server”属性的表单,这将生成viewstate输入,然后将表单提交到第二页时,它将尝试使用viewstate的详细信息填充页面,但是就像这样如果表单提交到另一页,则这两个页面不匹配,并且当它尝试处理viewstate输入时会引发错误。

The solution is to either remove the runat="server" attribute on the form, or to set EnableViewStateMac="False" attribute from the second page. 解决方案是删除表单上的runat =“ server”属性,或者从第二页设置EnableViewStateMac =“ False”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM