繁体   English   中英

jQuery验证JavaScript提交的表单

[英]jQuery Validate on form submitted by JavaScript

我的表单是通过使用JavaScript的链接提交的,但我也试图验证来自justing jQuery的验证。 链接提交时验证不起作用,但如果我将链接更改为提交按钮,则会生效。 我究竟做错了什么?

我的表格:

<form id="findmatch" method="post" action="search">
    <div>
        <label class="formlabel">Match Type
            <input type="text" name="matchtype" id="matchtype" class="forminput" />
        </label>

        <label class="formlabel">Location (postcode)
            <input type="text" name="location" id="location" class="forminput" />
        </label>

        <label class="formlabel">Radius (miles)
            <input type="text" name="Radius" id="Radius" class="forminput" />
        </label>

        <label class="formlabel">Keywords
            <input type="text" onblur="javascript:usePointFromPostcode(document.getElementById('location').value, showCompleteLatLng)" onchange="javascript:usePointFromPostcode(document.getElementById('location').value, showCompleteLatLng)" name="keywords" id="keywords" class="forminput" />
        </label>

        <input id="lat" class="hidden" name="lat" type="text" value="" />
        <input id="lon" class="hidden" name="lon" type="text" value="" />

        <a href="javascript:document.getElementById('findmatch').submit();" onmouseover="javascript:usePointFromPostcode(document.getElementById('location').value, showCompleteLatLng)" class="submit">Search</a>                        
    </div>
</form>

而我的jQuery是

<script type="text/javascript">
    $(document).ready(function () {
        $("#findmatch").validate({
            rules: {
                location: "required",
                Radius: {
                    required: true,
                    digits: true
                },
                keywords: "required"
            },
            messages: {
                location: "Please enter your postcode",
                Radius: {
                    required: "Please enter a radius",
                    digits: "Please only enter numbers"
                },
                keywords: "Please enter the keywords you wish to search for"
            }
        });
    });
</script>

演示: http //jsbin.com/urole/3

$(function () { 
  $('#submit').click(function(e) {
  e.preventDefault();
    $("#commentForm").submit(); 
  }); 
    $("#commentForm").validate();
});

提交

暂无
暂无

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

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