代码之家  ›  专栏  ›  技术社区  ›  Harsha M V

Ajax表单提交

  •  1
  • Harsha M V  · 技术社区  · 14 年前

    表单将作为普通请求而不是Ajax提交。

    $(document).ready(function () {
    
        StatusComments();
    
    });
    
    
    function StatusComments() {
    
        $('.comment').submit(function () {
            $(this).ajaxSubmit(options);
            return false;
        });
    
        var options = {
            beforeSubmit: showRequest,
            success: showResponse,
            resetForm: true
        };
    
        function showRequest(formData, jqForm, options) {
            var textbox = $('#StatusMessageReplyMessage').val();
            alert(textbox);
    
        }
    
        function showResponse(responseText, statusText, xhr, $form) {
    
        }
    
    }
    

    我有一个类似的状态更新

    function StatusUpdates() {
        $('#updateStatus').submit(function () {
            $(this).ajaxSubmit(options);
            return false; // prevent a new request
        });
    
        var options = {
            target: '.user-status',
            // target element(s) to be updated with server response 
            beforeSubmit: showRequest,
            // pre-submit callback 
            success: showResponse,
            // post-submit callback 
            // other available options: 
            //url:       url         // override for form's 'action' attribute 
            //type:      type        // 'get' or 'post', override for form's 'method' attribute 
            //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
            //clearForm: true        // clear all form fields after successful submit 
            resetForm: true // reset the form after successful submit 
            // $.ajax options can be used here too, for example: 
            //timeout:   3000 
        };
    
        function showRequest(formData, jqForm, options) {
    
            var textbox = $('#StatusMessageMessage').val();
            if ((textbox == '') || (textbox == "What have you been eating ?")) {
                alert('Please Enter Something and click submit.');
                return false;
            } else {
                $('#StatusMessageMessage').attr('disabled', true);
            }
    
        }
    
        function showResponse(responseText, statusText, xhr, $form) {
            $('#StatusMessageMessage').attr('disabled', false);
            $('.share').slideUp("fast");
            $('#StatusMessageMessage').animate({
                "height": "18px"
            }, "fast");
        }
    
    }
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   C. E.    14 年前

    而不是

     $('.comment').submit(function () {
            $(this).ajaxSubmit(options);
            return false;
        });
    

    尝试

     $('.comment').click(function(e) {
            e.preventDefault();
            $(this).ajaxSubmit(options);
            return false;
        });