javascript - 使用 js 验证表单的最简单/最佳方法是什么?

标签 javascript forms

目标:
我希望用户在表单中输入信息,然后我将使用该信息生成不同的结果。

这是我的代码:
http://jsfiddle.net/5HUVm/

<form id="form1" name="form1" method="post">
    <table>
        <tr>
            <td>I am interested in:</td>
            <td><input type="radio" name="choice" value="consume" id="consume"/> Cosuming and/or distributing OTC Markets data
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="radio" name="choice" value="extranet" id="extranet"/> Providing connectivity to OTC Markets(Extranet)</td>
        </tr>
        <tr>
            <td>Data products:</td>
            <td><input type="checkbox" name="realtime" value="realtime" id="realTime">Real-time data</td>
            <td><input type="checkbox" name="otcdata" value="otcdata" id="compData">OTC Company Data</td>
        </tr>
        <tr>
            <td></td>
            <td><input type="checkbox" name="eodprice" value="eodprice" id="eod">End of Day Pricing Data</td>
            <td><input type="checkbox" name="otccompliance" value="otccompliance" id="otcComp">OTC Compliance Data</td>
        </tr>
        <tr>
            <td></td>
            <td><input type="checkbox" name="otcsecurity" value="otcsecurity" id="otcSec">OTC Security Data</td>            
            <td><input type="checkbox" name="otcqhistory" value="otchistory" id="otcHist">OTC Quote History</td>
        </tr>
        <tr>

            <td>Data use:</td>
            <td>
                <select name="products" id="prod">
                    <option value="select">Select</option>
                    <option value="personal">Personal Use Only</option>
                    <option value="distEx">Distribute Externally Only</option>
                    <option value="distIn">Distribute Internally Only</option>
                    <option value="distBoth">Distribute Externally &amp; Internally Only</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Firm type:</td>
            <td>
                <select name="firm" id="firm">
                    <option value="select">Select</option>
                    <option value="broker">Broker-dealer</option>
                    <option value="oms">Order Management System</option>
                    <option value="finanInfo">Financial Information Service/Portal</option>
                    <option value="service">Service Bureau</option>
                    <option value="other">Other</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <input type="reset" id="re">
                <input type="button" id="sub" value="Submit" onclick="valid(this)">
            </td>
        </tr>
    </table>
</form>

如您所见,有复选框、单选按钮和下拉菜单。
验证已检查/选择的内容的最佳方法是什么?

我在想:
许多 if 语句然后使用一个变量来为不同的选择存储一个特殊的“数字”,然后这个数字将被用来生成结果页面

例如:
用户选择消费、实时数据和一天结束的数据,可以说是选择 1 用户点击提交,结果页面将显示特殊链接/文章。

最佳答案

有很多验证脚本可以完全按照您的意愿进行操作 - 不要重新发明轮子....去拿一个并使用它:

原型(prototype):http://tetlaw.id.au/view/blog/really-easy-field-validation-with-prototype

JQuery : http://bassistance.de/jquery-plugins/jquery-plugin-validation/

唯:http://www.yuiblog.com/blog/2009/12/03/yui-3-gallery-form-module/

关于javascript - 使用 js 验证表单的最简单/最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7714346/

相关文章:

php - 从 Ajax 脚本重定向 Javascript 页面

javascript - 使用tinymce禁用/启用按钮

javascript - jQuery 更新 (1.7) 打破了触摸事件的事件坐标?

c# - 除了计算距离之外,如何衡量 2 个字符串的相似性

java - 在没有字段绑定(bind)的情况下使用表单错误?

javascript - AngularJS,ng-repeat 不重复

javascript - 如何获取div元素中的textarea值?

java - NumberFormatException -- 验证 JSP 文件中的表单提交?

java - 在表单之间转换时,Form Fusion 应用程序会导致白色闪烁。仅在 WebLogic 中,不在 Oracle 应用服务器中

php - 通过 javascript 添加的输入字段不在 PHP $_POST 变量中。如何解决这个问题?