【问题标题】:HTML with JavaScript Form validation and JQuery Mobile带有 JavaScript 表单验证和 JQuery Mobile 的 HTML
【发布时间】:2014-05-23 04:02:10
【问题描述】:

在 HTML 表单中,我使用 JavaScript 表单验证来避免出现空字段。这是表单的代码:

 <form method="post" name="form1" onsubmit="return validateForm()" action="<?php echo $editFormAction; ?>">
        <table align="center">
          <tr valign="baseline">
            <td nowrap align="right">Nickname:</td>
            <td>
              <input type="text" name="nickname" value="" size="32">
            </td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right">Email:</td>
            <td><input type="text" name="email" value="" size="32"></td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right">Password:</td>
            <td><input type="text" name="password" value="" size="32"></td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right">&nbsp;</td>
            <td><input type="submit" value="Insert record"></td>
          </tr>
        </table>
        <input type="hidden" name="estado" value="0">
        <input type="hidden" name="MM_insert" value="form1">
      </form>

这是 JavaScript 函数:

<script>
function validateForm()
{
var x=document.forms["form1"]["nickname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
</script>

正如预期的那样,如果用户单击提交按钮并且“昵称”字段为空,则会显示警报对话框,但在关闭它后,会提交表单。我在我的网络中使用 Dreamweaver 作为编辑器和 JQuery Mobile,可能是这个问题。

欢迎任何帮助。

【问题讨论】:

  • 它在 chrome 上运行良好..
  • @user3091574,谢谢,我使用的是 Safari 和 Chrome,在这两种浏览器上都会显示警报对话框,但关闭它后,表单已提交。
  • @user3091574,这是问题所在,但我想问题出在我的 Web 的 JQuery Mobile 端,而不是 JavaScript 表单验证。
  • 我解决了你的问题,请在下面查看我的答案。

标签: html forms jquery-mobile


【解决方案1】:

工作示例:http://jsfiddle.net/Gajotres/vds2U/50/

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" /> 
        <!--<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>-->
        <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>    
    </head>
    <body>     
        <div data-role="page" id="index" data-theme="a" >
            <div data-role="header">
                <h3>
                    First Page
                </h3>
                <a href="#second" class="ui-btn-right">Next</a>
            </div>

            <div data-role="content">
                <form method="post" id="form1" name="form1"  action="" data-ajax="false">
                    <table align="center">
                        <tr valign="baseline">
                            <td nowrap align="right">Nickname:</td>
                            <td>
                                <input type="text" name="nickname" value="" size="32"/>
                            </td>
                        </tr>
                        <tr valign="baseline">
                            <td nowrap align="right">Email:</td>
                            <td><input type="text" name="email" value="" size="32"/></td>
                        </tr>
                        <tr valign="baseline">
                            <td nowrap align="right">Password:</td>
                            <td><input type="text" name="password" value="" size="32"/></td>
                        </tr>
                        <tr valign="baseline">
                            <td nowrap align="right">&nbsp;</td>
                            <td><input type="submit" value="Insert record"/></td>
                        </tr>
                    </table>
                    <input type="hidden" name="estado" value="0"/>
                    <input type="hidden" name="MM_insert" value="form1"/>
                </form>             
            </div>

            <div data-role="footer" data-position="fixed">

            </div>
        </div> 
        <div data-role="page" id="second" data-theme="a" >
            <div data-role="header">
                <h3>
                    Second Page
                </h3>
                <a href="#index" class="ui-btn-left">Back</a>
            </div>

            <div data-role="content">

            </div>

            <div data-role="footer" data-position="fixed">

            </div>
        </div>  
    </body>
</html>   

JavaScript:

$(document).on('submit', '#form1', function(){ 
    var x=document.forms["form1"]["nickname"].value;
    if (x==null || x=="") {
        alert("First name must be filled out");
        return false;
    } else {
        return true;    
    }
});

变化:

  • jQuery Mobile 有自己的表单处理方式,如果你想要经典的表单处理,你需要禁用它。这是通过属性 data-ajax="false" 完成的。
  • 永远不要在 jQuery Mobile 中使用内联 javascript,我的意思是永远不要。

【讨论】:

  • 谢谢,插入 data-ajax="false" 后,表单不提交,直到字段不为空。再次感谢您。
【解决方案2】:

对我来说这很好:

<script>
        function validateForm()
        {
            var x=document.forms["form1"]["nickname"].value;
            if (x==null || x=="")
            {
                alert("First name must be filled out");
                return false;
            }else{
                return true;
            }
        }
    </script>

刚刚添加了一个 else 语句。它对我来说很好。

【讨论】:

  • 谢谢,无论是否放置 else 语句,javascript 都可以正常工作,但在关闭警报对话框后,两种情况下都会提交表单。我确定问题出在 JQuery Mobile 上。
【解决方案3】:

我认为您的代码应该可以工作。但如果不是,您可以进行一些更改为

更改 1 从标签中删除提交事件

 <form method="post" name="form1"  action="<?php echo $editFormAction; ?>">

更改 2。

将提交按钮更改为默认按钮并在此处验证事件

<input type="button" onclick="javascript:validateForm();" value="Insert record">

更改 3。 现在在 javascript 中更改添加代码表单子

<script>
                    function validateForm()
        {
            var x=document.forms["form1"]["nickname"].value;
            if (x==null || x=="")
            {
                alert("First name must be filled out");

            }else{
                   document.forms["form1"].submit();
            }
        }
            </script>

【讨论】:

  • 谢谢,但我的问题有更好的答案。
猜你喜欢
  • 1970-01-01
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多