weixin_33721344 2016-06-13 09:52 采纳率: 0%
浏览 17

Ajax提交某些字段

I'm new to Jquery ajax, and I'm trying to submit certain field only for checking instead of submitting the whole form. Here I have made a function for checking the username whether is it available, and it works fine, but I doesn't want it to submit the whole form when doing the checking.

Here is my script:

<form id="userCheck">
<input type="text" class="register_field" name="fr_username" id="fr_username" />
<input type="text" class="register_field" name="fr_password" id="fr_password" />
<input type="text" class="register_field" name="fr_password1" id="fr_password1" />
</form>

<script>
$.validator.addMethod("duplicateCheck", function(value, element) {
            $.ajax({
        type: 'POST',
        url: '../a/checkDuplicate',
        data: $("#userCheck").serialize(),
        dataType: 'json'
    }).success(function(data){
        if(data.status == '1'){
            console.log('Available!');
        }
        else {
            console.log('not available');
            $('#fr_dupChkU_msg').html('');
        }
    });
        });
</script>
  • 写回答

1条回答 默认 最新

  • local-host 2016-06-13 10:04
    关注

    In short, that is how you get a value of an input field with jQuery:

    <script type="text/javascript">
      var username = $('#fr_username').val();
    
      $.validator.addMethod('duplicateCheck', function(value, element) {
                $.ajax({
            type: 'POST',
            url: '../a/checkDuplicate',
            data: {username: username},
        }).success(function(data){
            if(data.status == '1'){
                console.log('Available!');
            }
            else {
                console.log('not available');
                $('#fr_dupChkU_msg').html('');
            }
        });
            });
    </script>
    
    评论

报告相同问题?