【问题标题】:My submit button does not functioning [closed]我的提交按钮不起作用[关闭]
【发布时间】:2017-04-11 13:43:17
【问题描述】:

我尝试使用该 java 脚本提交我的表单。但它没有按应有的方式工作,它没有从脚本中获取值。它仅适用于我的 html 正文中的警报编码,但它没有读取函数 check(),我的表单有问题吗?

<form name="myForm" method="post" onsubmit="return check()">
                        <table width="100%">
                        <tr>
                            <div id="space">
                                YOUR DETAILS 
                            </div><!-- space -->
                        </tr>
                        <tr>
                            <td width="186"><span style="color:red;">*</span>First Name</td>
                            <td width="720">
                            <input type="text" name="fname">
                            </td>
                        </tr>
                        <tr>
                            <td width="186"><span style="color:red;">*</span>Last Name</td>
                            <td width="720">
                            <input type="text" name="lname">
                            </td>
                        </tr>
                        <tr>
                            <td width="186"><span style="color:red;">*</span>Email</td>
                            <td width="720">
                            <input type="email" name="email">
                            </td>
                        </tr>
                        <tr>
                            <td width="186"><span style="color:red;">*</span>Telephone</td>
                            <td width="720">
                            <input type="tel" name="telephone">
                            </td>
                        </tr>
                        <tr>
                            <td width="186">Fax</td>
                            <td width="720">
                            <input type="tel" name="fax">
                            </td>
                        </tr>
                        <tr>
                            <td width="186">Company</td>
                            <td width="720">
                            <input type="text" name="company">
                            </td>
                        </tr>
                        <tr>
                            <td width="186">Company ID</td>
                            <td width="720">
                            <input type="text" name="cid">
                            </td>
                        </tr>
                        <tr>
                            <td width="186"><span style="color:red;">*</span>Address 1</td>
                            <td width="720">
                            <input type="text" name="add1">
                            </td>
                        </tr>
                        <tr>
                            <td width="186">Address 2</td>
                            <td width="720">
                            <input type="text" name="add2">
                            </td>
                        </tr>
                        <tr>
                            <td width="186"><span style="color:red;">*</span>City</td>
                            <td width="720">
                            <input type="text" name="city" >
                            </td>
                        </tr>
                        <tr>
                            <td width="186">Poscode</td>
                            <td width="720">
                            <input type="text" name="poscode" >
                            </td>
                        </tr>
                        <tr>
                            <td width="186"><span style="color:red;">*</span>Country</td>
                            <td width="720">
                                <select name="country">
                                    <option value="malaysia">Malaysia</option>
                                    <option value="australia">Australia</option>
                                    <option value="japan">Japan</option>
                                    <option value="newzealand">New Zealand</option>
                                </select>
                            </td>
                        </tr>
                        </table>

                        <br><br>

                        <input type="button" onclick="check()" name="submit" value="Submit" class="sbutton" />
                        <button name="subcancel" class="sbutton" value="Cancel" >CANCEL</button>
                        </form>

这是我的javascript函数

function check(){
alert('hi');
var isi1=document.forms["myForm"]["fname"].value;
var isi2=document.forms["myForm"]["lname"].value;
var isi3=document.forms["myForm"]["email"].value;
var isi4=document.forms["myForm"]["tel"].value;
var isi5=document.forms["myForm"]["add1"].value;
var isi6=document.forms["myForm"]["city"].value;
var isi7=document.forms["myForm"]["country"].value;

if (isi1 == "") {
     alert("Please complete all your detail with '*' symbol!");
     return false;
}
else if(isi2 ==""){
     alert("Please complete your detail!");
     return false;
}
else if(isi3 ==""){
     alert("Please complete your detail!");
     return false;
}
else if(isi4 ==""){
     alert("Please complete your detail!");
     return false;
}
else if(isi5 ==""){
     alert("Please complete your detail!");
     return false;
}
else if(isi6 ==""){
     alert("Please complete your detail!");
     return false;
}
else{
    alert("Hi "+isi1+" "+isi2+"!! You are succesfully registered to our bookstore!!");
    return true;
}

}

【问题讨论】:

  • Javascript != Java
  • 好吧,你不要取消点击
  • 标记中删除 onsubmit="return check()"。将 check() 事件处理程序添加到按钮按下。使用 AJAX 手动提交或添加表单 POST 目标和form.submit() 您可能无法从 check() 处理程序返回以阻止提交发生。所以用if (isValid) form.submit() ps 之类的逻辑替换它:你可以遍历所有这些值来检查它们是否不是空字符串。

标签: javascript html form-submit submit-button


【解决方案1】:

检查下面的工作代码:

     function check() {
        alert('hi');
        var isi1 = document.forms["myForm"]["fname"].value;
        var isi2 = document.forms["myForm"]["lname"].value;
        var isi3 = document.forms["myForm"]["email"].value;
        var isi4 = document.forms["myForm"]["telephone"].value;
        var isi5 = document.forms["myForm"]["add1"].value;
        var isi6 = document.forms["myForm"]["city"].value;
        var isi7 = document.forms["myForm"]["country"].value;

        if (isi1 == "") {
            alert("Please complete all your detail with '*' symbol!");
            return false;
        }
        else if (isi2 == "") {
            alert("Please complete your detail!");
            return false;
        }
        else if (isi3 == "") {
            alert("Please complete your detail!");
            return false;
        }
        else if (isi4 == "") {
            alert("Please complete your detail!");
            return false;
        }
        else if (isi5 == "") {
            alert("Please complete your detail!");
            return false;
        }
        else if (isi6 == "") {
            alert("Please complete your detail!");
            return false;
        }
        else {
            alert("Hi " + isi1 + " " + isi2 + "!! You are succesfully registered to our bookstore!!");
            return true;
        }
    }
<form name="myForm" method="post" onsubmit="return check()">
<table width="100%">
    <tr>
        <div id="space">
            YOUR DETAILS
        </div><!-- space -->
    </tr>
    <tr>
        <td width="186"><span style="color:red;">*</span>First Name</td>
        <td width="720">
            <input type="text" name="fname">
        </td>
    </tr>
    <tr>
        <td width="186"><span style="color:red;">*</span>Last Name</td>
        <td width="720">
            <input type="text" name="lname">
        </td>
    </tr>
    <tr>
        <td width="186"><span style="color:red;">*</span>Email</td>
        <td width="720">
            <input type="email" name="email">
        </td>
    </tr>
    <tr>
        <td width="186"><span style="color:red;">*</span>Telephone</td>
        <td width="720">
            <input type="tel" name="telephone">
        </td>
    </tr>
    <tr>
        <td width="186">Fax</td>
        <td width="720">
            <input type="tel" name="fax">
        </td>
    </tr>
    <tr>
        <td width="186">Company</td>
        <td width="720">
            <input type="text" name="company">
        </td>
    </tr>
    <tr>
        <td width="186">Company ID</td>
        <td width="720">
            <input type="text" name="cid">
        </td>
    </tr>
    <tr>
        <td width="186"><span style="color:red;">*</span>Address 1</td>
        <td width="720">
            <input type="text" name="add1">
        </td>
    </tr>
    <tr>
        <td width="186">Address 2</td>
        <td width="720">
            <input type="text" name="add2">
        </td>
    </tr>
    <tr>
        <td width="186"><span style="color:red;">*</span>City</td>
        <td width="720">
            <input type="text" name="city">
        </td>
    </tr>
    <tr>
        <td width="186">Poscode</td>
        <td width="720">
            <input type="text" name="poscode">
        </td>
    </tr>
    <tr>
        <td width="186"><span style="color:red;">*</span>Country</td>
        <td width="720">
            <select name="country">
                <option value="malaysia">Malaysia</option>
                <option value="australia">Australia</option>
                <option value="japan">Japan</option>
                <option value="newzealand">New Zealand</option>
            </select>
        </td>
    </tr>
</table>

<br><br>

<input type="button" onclick="check()" name="submit" value="Submit" class="sbutton" />
<button name="subcancel" class="sbutton" value="Cancel">CANCEL</button>
</form>

【讨论】:

  • 谢谢,它突然在我的浏览器中工作了。您只需在 javascript 中添加警报吗?
  • 问题出现在这一行 var isi4=document.forms["myForm"]["tel"].value;错误的名称“电话”
猜你喜欢
  • 2015-03-11
  • 2013-04-09
  • 1970-01-01
  • 2019-12-11
  • 2017-05-20
  • 2017-03-13
相关资源
最近更新 更多