表单响应更改值

时间:2014-10-16 13:55:46

标签: javascript jquery html validation

我试图让它成为当你提交我的表格时 - 如果你输入名字中的'Martin',姓氏中的'Neuhard',并在下拉列表中选择'Binghamton University',它会改变背景绿色和弹出窗口宾汉姆顿大学的一个小调。如果我使用||,它可以工作但不是当我使用&&。

另外我想这样做,如果两个电子邮件字段不一样那么警告('错误')

我已经尝试了几个小时,但无法理解。 jsfiddle在

之下

http://jsfiddle.net/esaunde1/ngjo8aoq/

HTML

        <div class="container" id='contact'>
        <div class="row">
          <div class="col-md-8 col-md-offset-2">
            <div class="well well-sm">
              <form class="form-horizontal" action="send_form_email.php" method="post">
              <fieldset>
                <legend class="text-center">Get in touch!</legend>

                <div class="form-group">
                  <label class="col-xs-4 control-label" for="name">First Name</label>
                  <div class="col-xs-8">
                    <input id="name" name="name" type="text" placeholder="Your first name" data-    trigger="manual" data-content="Can only contain letters!" class=" form-control" pattern="[A-Za-z]+" type="text" required>
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-xs-4 control-label" for="lname">Last Name</label>
                  <div class="col-xs-8">
                    <input id="lname" name="lname" type="text" placeholder="Your last name" data-trigger="manual" data-content="Can only contain letters!" class="name form-control" type="text" required pattern="[A-Za-z]+">
                  </div>
                </div>


                <div class="form-group">
                  <label class="col-xs-4 control-label" for="email">E-mail Address</label>
                  <div class="col-xs-8">
                    <input id="email" name="email" type="email" placeholder="What's your email?" class="email form-control" data-placement="top" data-trigger="manual" data-content="Must be a valid e-mail address" type="text" email required>
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-xs-4 control-label" for="emailCheck">E-mail Address</label>
                  <div class="col-xs-8">
                    <input id="emailCheck" name="emailCheck" type="email" placeholder="What's your email?" class="email form-control" data-placement="top" data-trigger="manual" data-content="Must be a valid e-mail address" type="text" email required>
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-xs-4 control-label" for="email">Your Phone Number</label>
                  <div class="col-xs-8">
                    <input id="email" name="email" placeholder="What is your phone number?" class="form-control phone" data-placement="top" data-trigger="manual" data-content="Must be a valid phone number" type="text" pattern="[0-9]+" required>
                  </div>
                </div>

                 <div class="form-group">
                    <label class='col-xs-4 control-label'>What school do you go to?</label>
                    <div class='col-xs-8'>
                        <select class="form-control" id='dropdown'>
                          <option>Select One</option>
                          <option>The College of NJ</option>
                          <option>Rutgers University</option>
                          <option>Columbia University</option>
                          <option value="bing">Binghamton University</option>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                  <div class="col-md-12">

                    <input class="btn btn-primary" value="Check Form" onClick='return valid()'>
                  </div>
                </div>

              </fieldset>
              </form>
            </div>
          </div>
        </div>
    </div>

JAVASCRIPT

function valid(){   

var name = document.getElementById('name');
var lname = document.getElementById('lame');
var home = document.getElementById('home');

    if (name.value == 'Martin' && lname.value == 'Neuhard' && ){
        $('#home').addClass('green');
        $('#contact').addClass('image');

        }
    else if (document.getElementById('first').value == 'martin'){
        alert('thats our profs name!')
    }
    else return false;

}

1 个答案:

答案 0 :(得分:0)

像这样的东西

$(document).ready(function () {
    $('#check-btn').click(function (e) {
        e.preventDefault();
        var $form = $('form');
        var fName = $('#name', $form).val();
        var lName = $('#lname', $form).val();
        var school = $('#dropdown', $form).val();
        if(fName == 'Martin' && lName == 'Neuhard' && school == 'bing'){
            //do something here
            $('#contact').addClass('green');
        }
        else if(fName == 'martin'){
            alert('thats our profs name!')
        }

        //if the 2 emails are not the same
        var email = $('#email', $form).val();
        var repeatEmail = $('#emailCheck', $form).val();
        if(email != repeatEmail){
            alert('Email does not match!');
        }
    });
});

http://jsfiddle.net/ngjo8aoq/3/