jQuery验证JavaScript提交的表单

时间:2010-05-05 13:50:40

标签: javascript jquery validation forms submit

我的表单是通过使用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>

1 个答案:

答案 0 :(得分:1)

DEMO: http://jsbin.com/urole/3

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

提交