1

我有这个几乎可以工作的位:http: //jsfiddle.net/HFzkW/ 我的代码如下所示:

<div id="cform-container">
<div class="alignleft" id="cform">
    <form action="<?php print site_url('/sent'); ?>" method="POST" >
    <input type="hidden" name="Contact-Form" value="contact-form" />
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="yn">Your Name</label><span id="star5">*</span></div>
            <div class="alignright cform-ti"><input type="text" name="yn" id="yn" /><br /><span id="namespan" value="0"></span></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="cn">Company Name</label></div>
            <div class="alignright cform-ti"><input type="text" name="cn" id="cn" /></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="em">Email Address</label><span id="star5">*</span></div>
            <div class="alignright cform-ti"><input type="text" name="em" id="em" /><br /><span id="emailspan" value="0"></span></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="ph">Phone Number</label><span id="star5">*</span></div>
            <div class="alignright cform-ti"><input type="text" name="ph" id="ph" /><br /><span id="phonespan" value="0"></span></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="ct">Industry</label></div>
            <div class="alignright cform-ti"><input type="text" name="ct" id="ct" /></div>
            <div class="clearboth"></div>
        </div>
        <div class="cform-row">
            <div class="alignleft cform-label"><label for="msg">Message</label></div>
            <div class="alignright cform-ti"><textarea name="msg" id="msg"></textarea></div>
            <div class="clearboth"></div>
        </div>
        <div id="cform-btn"><input type="submit" value="Submit" /></div>
    </form>
</div>
<script id="source" language="javascript" type="text/javascript">
 $(function(){  
    $("input[type=submit]").attr("disabled", "disabled");
    $("#em").blur(function() 
            {
             var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
             var emailaddress = $("#em").val();
             if(!emailReg.test(emailaddress) | emailaddress=="") {
                $("#emailspan").html('<font color="#cc0000">Please enter valid Email address</font>');
             }else{
                $("#emailspan").html('<font color="#cc0000"></font>');  
                }
            });
    $("#yn").blur(function() 
            {
             var namefield = $("#yn").val();
             if(namefield=="") {
                $("#namespan").html('<font color="#cc0000">Please enter your name</font>');  
             }else{
                $("#namespan").html('<font color="#cc0000"></font>');  
                }
            });                                 
    $("#ph").blur(function() 
            {
             var phonefield = $("#ph").val();
             if(phonefield=="") {
                $("#phonespan").html('<font color="#cc0000">Please enter your name</font>');  
             }else{
                $("#phonespan").html('<font color="#cc0000"></font>');  
                }
            });
$('input, textarea').change(function(){
    var validation;
    var email = $("#em").val();
    var name = $("#yn").val();
    var phone = $("#ph").val();

    if ( email == "" && name == "" && phone == "" ) {
    validation = false;
    } else if ( email != "" && name != "" && phone != "" ) {
    alert ("variables"+email+name+phone);
    validation = true;
    }
    if (validation = true) {
    $("input[type=submit]").removeAttr("disabled"); 
    } else {
    $("input[type=submit]").attr("disabled", "disabled");
    }
});
});

但是我不能让它完全工作。当用户输入其中一个字段(例如您的姓名)时,提交按钮不再被禁用。

我想保持禁用提交按钮,直到满足所有 3 个“必填”字段。我认为代码对此非常清楚 - 但它仍然无法正常工作。

此外,它可以在 JSFiddle 中工作,但不能在我试图让它工作的 Wordpress 网站上工作。任何检查的想法也将不胜感激。

4

3 回答 3

3

尝试将 ands 切换为 ors。小心你的逻辑,试着大声说出来,看看它是否有意义:

电子邮件为空白,姓名为空白,电话为空白,则验证为假。您真正想要的是说如果任何字段为空白,则验证为假。

if ( email == "" || name == "" || phone == "" ) {
validation = false;
} else if ( email != "" && name != "" && phone != "" ) {
alert ("variables"+email+name+phone);
validation = true;
}

此外,您正在设置validation=trueif 语句。这将始终返回 true。改成这样:if (validation == true) {

于 2013-04-22T22:27:10.270 回答
0

所以基本上:

<form id="myForm" onfocus="submitButton("myForm);">
  <input type="text" name="name" placeholder="Name">
  <input type="text" name="email" placeholder="Email">
  <input type="text" name="phone" placeholder="Phone">
</form>

function submitButton(formID){

  var formObject = document.getElementById(formID);

  var name = formObject.name.value;
  var email = formObject.email.value;
  var phone = formObject.phone.value;

  if (name && email && phone) {
    showSubmitButton();
  } else {
    //keep the button hidden
  }
}
于 2013-04-22T22:45:54.210 回答
0

从我所看到的,我会做的第一件事是调试它(代码有点混乱,所以不要复制它,我会给你一些指导)。

代替

 if(validation = true )

尝试

 if(validation)

这是因为在没有任何参数的情况下,它将检查是否为 false、未定义或 null。电子邮件名称和电话也可以这样做。

于 2013-04-22T22:31:54.733 回答