开发者

Pre-filled input text (watermark) - how to prevent value from being submitted via jQuery Ajax?

开发者 https://www.devze.com 2023-03-14 05:30 出处:网络
I am using the jQuery Example plugin to pre-fill text input fields of a form. It works nicely, by clearing the input on focus and whatever you type in there is preserved on blur.

I am using the jQuery Example plugin to pre-fill text input fields of a form.

It works nicely, by clearing the input on focus and whatever you type in there is preserved on blur.

The documentation for the plugin says that it will prevent submitting the pre-fill text at submission. But there is no comment on what happens when submitting via Ajax.

When I submit my form (via Ajax), the pre-fill text is actually sent to the server.

I wonder if anyone can help with ideas on how to empty value on ajax if the input field is empty or contains the pre-fill text.

Thanks for helping.

Thi开发者_如何学运维s is the form

<form action="http://example.com/1" method="post" accept-charset="utf-8" id="posts_form" enctype="multipart/form-data">

<input type="text" name="posts_text" value="" id="posts_text" class="example">

</form>

jQuery

$('#posts_text').example('What\'s on your mind?');

function submitPost() {

    $.ajax({
        url: 'chat/posts_submit/' + <?php echo $page_id; ?>,
        type: 'POST',
        data: $('#posts_form').serialize(),
        dataType: 'html',
        beforeSend: function(){
              $('#loading').show();
            },          
        success: function(html) {
            $('#posts_insert').replaceWith(html);
            $('#loading').hide();
        }
    });
}

$('.share_js').click(function(e){
    e.preventDefault();
    submitPost();
    return false; // dont move to top
});


You can check if it says "What's on your mind?" and ask the user to enter something with this code:

function submitPost(){
    if($("#posts_text").val() == "What's on your mind?"){
        alert("Please enter what's on your mind...");
    }else{
        // Continue on submitting via Ajax
    }
}

Ad@m

0

精彩评论

暂无评论...
验证码 换一张
取 消