停止提交表格

时间:2010-11-14 09:29:40

标签: javascript jquery validation

我有一个表单,提交时应查看文本区域是否没有文本或占位符文本。

如果它确实不应该提交它。像验证一样。我无法停止表单提交。

$(document).ready(function () {
    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 
    };

    $('#updateStatus').submit(function () {
        $(this).ajaxSubmit(options);
        return false; // prevent a new request
    });

    function showRequest(formData, jqForm, options) {

        var textbox = $('#StatusMessageMessage').val();
        if ((textbox == '') || (textbox == "What have you been eating ?")) {
            alert(text);
            return false;
        } else {
            $('#StatusMessageMessage').attr('disabled', true);
        }

    }

    function showResponse(responseText, statusText, xhr, $form) {
        $('#StatusMessageMessage').attr('disabled', false);
    }

    statusMEssagebox();


});



function statusMEssagebox() {
    var textholder = $('#StatusMessageMessage').val();

    $('#StatusMessageMessage').focus(function () {
        if ($(this).val() == textholder) $(this).val("");

        $(this).animate({
            "height": "48px",
        }, "fast");
        $('.share').slideDown("fast");
        $(this).TextAreaExpander(48, 75);

    });

    $('#StatusMessageMessage').blur(function () {
        if ($(this).val() == "") {
            $(this).val(textholder);
            $('.share').slideUp("fast");
            $(this).animate({
                "height": "18px",
            }, "fast");
        }
    });
}

2 个答案:

答案 0 :(得分:1)

您使用的是等于运算符而不是比较。

此:

if ((textbox = '') || (textbox = "What have you been eating ?")) {
Problem -----^

应该是:

if (textbox == '' || textbox == "What have you been eating ?") {

您可能还想看看使用jQuery通过Ajax提交表单的方式:

答案 1 :(得分:1)

我看到你正在使用jquery表单插件来激活你的表单。文档中有example,说明如何以优雅的方式实现此目的。您无需订阅表单的.submit事件,并使用.ajaxSubmmit手动提交。你可以试试这个:

$(function() {
    $('#updateStatus').ajaxForm({ 
        beforeSubmit: function(formData, jqForm, options) {
            var value = $('#StatusMessageMessage').val();
            if (value == '' || value == 'What have you been eating ?') {
                return false;
            }
        }
    });
});