为什么不能提交表格? [英] why the form cannot be submit?

查看:110
本文介绍了为什么不能提交表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在这里,当单击提交按钮时,表单无法发布.我在js中添加了一些代码

my code is here,when the submit button clicked ,the form cannot be post.and i add some code in js

else if (oCheck[i].type == 'button') {
               oCheck[i].onclick = function () {
                   alert("ddddd");
               }
           }


看看按钮发生了什么,但是预期的消息框"ddddd"甚至没有弹出.我的代码有什么问题?


to see what happened with the button ,but the expected messagebox "ddddd" was not even popup.what''s wrong with my code?

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        
       <div id="somecheckbox">
       <input type="checkbox" value="somevalue" />
       <input type="checkbox" value="somevalue" />
       <input type="checkbox" value="somevalue" />
       </div>

	<input type="text" id="edittobeset" />
        <p>
            <input type="submit" value="submit" />
            <input type="reset"  value="reset" />
        </p>
    </fieldset>
}

<script type="text/javascript">

    window.onload = function () {
        var divCheck = document.getElementById("somecheckbox");
        var oCheck = divCheck.getElementsByTagName("input");
        var oSet = document.getElementById("edittobeset");
        
        for (var i = 0; i < oCheck.length; i++) {
            if (oCheck[i].type == 'checkbox') {
                oCheck[i].onclick = function () {

                    if (this.checked == true) {
                        oSet.value += this.value + ".";
                    }
                }
            }
            else if (oCheck[i].type == 'button') {
                oCheck[i]. önclick = function () {
                    alert("ddddd");
                }
            }
        }
   }
</script>

推荐答案

您遇到的问题是您要查找的控件不在收集您的迭代.

for循环:
The problem you have is the control you''re looking for is not in the collection your iterating.

The for loop:
for (var i = 0; i < oCheck.length; i++)


正在检查oCheck对象的控件. oCheck是:


Is checking the oCheck object for controls. oCheck is:

var oCheck = divCheck.getElementsByTagName("input");


divCheck是:


divCheck is:

var divCheck = document.getElementById("somecheckbox");


这意味着,在您的for循环中,您将检查输入元素的"somecheckbox"的内容.您要查找的输入元素不是"somecheckbox"的子元素,因此不会成为oCheck集合的一部分.

由于您只定位了一个提交按钮,因此我将代码更改为以下内容:


This means, in your for loop, you checking the contents of ''somecheckbox'' for input elements. The input element you''re trying to find isn''t a child to ''somecheckbox'' and as such would not part of the oCheck collection.

As you''re only targeting a single submit button I''d change the code to the following:

window.onload = function () {
       var divCheck = document.getElementById("somecheckbox");
       var oCheck = divCheck.getElementsByTagName("input");
       var oSet = document.getElementById("edittobeset");

       for (var i = 0; i < oCheck.length; i++) {
           if (oCheck[i].type == 'checkbox') {
               oCheck[i].onclick = function () {

                   if (this.checked == true) {
                       oSet.value += this.value + ".";
                   }
               }
           }
       }

       var oSubmit = document.getElementsByTagName('submit')[0];
       oSubmit. önclick = function () {
                   alert("ddddd");
               }

  }


我犯了一个错误.在我的动作函数名称中输入错误的字母.
i made a mistake. type a wrong letter in my action function name.


这篇关于为什么不能提交表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆