0

它工作的联系表,如果你填写它,它会发送消息。如果您不填写电子邮件框,表格不会提醒您,问题是我可以向用户显示一个单词或某种提醒吗?

这是我的标记:

<div class="form">
        <h2>ESCRIBENOS</h2>
        <form method="post" action="process.php">
        <div class="element">
            <label>Nombre (obligatorio):</label><br/>
            <input type="text" name="name" class="text" />
        </div>
        <div class="element">
            <label>Email (obligatorio):</label><br/>
            <input type="text" name="email" class="text" />
        </div>
        <div class="element">
            <label>Telefono:</label><br/>
            <input type="text" name="website" class="text" />
        </div>
        <div class="element">
            <label>Mensaje:</label><br/>
            <textarea name="comment" class="text textarea" /></textarea>
        </div>
        <div class="element">

            <input type="submit" id="submit"/>
            <div class="loading"></div>
        </div>
        </form>
        </div>

这是我的脚本:

$(document).ready(function() {

//if submit button is clicked
$('#submit').click(function () {        

    //Get the data from all the fields
    var name = $('input[name=name]');
    var email = $('input[name=email]');
    var website = $('input[name=website]');
    var comment = $('textarea[name=comment]');

    //Simple validation to make sure user entered something
    //If error found, add hightlight class to the text field
    if (name.val()=='') {
        name.addClass('hightlight');
        return false;
    } else name.removeClass('hightlight');

    if (email.val()=='') {
        email.addClass('hightlight');
        return false;
    } else email.removeClass('hightlight');

    if (comment.val()=='') {
        comment.addClass('hightlight');
        return false;
    } else comment.removeClass('hightlight');

    //organize the data properly
    var data = 'name=' + name.val() + '&email=' + email.val() + '&website=' + 
    website.val() + '&comment='  + encodeURIComponent(comment.val());

    //disabled all the text fields
    $('.text').attr('disabled','true');

    //show the loading sign
    $('.loading').show();

    //start the ajax
    $.ajax({
        //this is the php file that processes the data and send mail
        url: "../process.php",  

        //GET method is used
        type: "GET",

        //pass the data         
        data: data,     

        //Do not cache the page
        cache: false,

        //success
        success: function (html) {              
            //if process.php returned 1/true (send mail success)
            if (html==1) {                  
                //hide the form
                $('.form').fadeOut('slow');                 

                //show the success message
                $('.done').fadeIn('slow');

            //if process.php returned 0/false (send mail failed)
            } else alert('Sorry, unexpected error. Please try again later.');               
        }       
    });

    //cancel the submit button default behaviours
    return false;
}); 
}); 

有人可以帮帮我吗?

4

1 回答 1

2

试试这个:

var name = $('input[name=name]');
        var email = $('input[name=email]');
        var website = $('input[name=website]');
        var comment = $('textarea[name=comment]');

        //Simple validation to make sure user entered something
        //If error found, add hightlight class to the text field

        $('input[type=text]').each(function(){
             if($(this).val().length == 0){
                $(this).addClass('hightlight');
                alert('Empty input field')
                return false;
             }
        });

....您的其余代码

注意:这不适用于 textarea 但我认为您可以自己弄清楚!


编辑:

  var valid = false;

    $('input[type=text]').each(function(){
             if($(this).val().length == 0){
                $(this).addClass('hightlight');
                alert('Empty input field')
                valid = false;
             }else{
                valid = true;
             }
        });


    if(valid == false) return; 
        console.log('All input fields are filled in..');

...您的其余代码。您可以删除所有输入字段的 if else 语句。为了检查文本区域,您可以为所有字段提供相同的类并执行以下操作:

$('form.classofallelements').each(function(){
于 2012-11-08T09:07:30.197 回答