我有这个几乎工作的位: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网站上工作,我试图让它工作。任何检查的想法也将受到赞赏。
答案 0 :(得分:3)
尝试切换到ors。小心你的逻辑并试着大声说出它是否有意义:
电子邮件为空,名称为空白且手机为空,则验证为空。您真正想要的是说如果任何字段为空,则验证为假。
if ( email == "" || name == "" || phone == "" ) {
validation = false;
} else if ( email != "" && name != "" && phone != "" ) {
alert ("variables"+email+name+phone);
validation = true;
}
此外,您在if语句中设置了validation=true
。这将永远返回true。更改为:if (validation == true) {
答案 1 :(得分:0)
从我所看到的,我要做的第一件事就是调试这个(代码有点令人困惑,所以请复制它,我会给你一些指导)。
而不是
if(validation = true )
试
if(validation)
这是因为没有任何参数,它将检查不是false,undefined或null。 电子邮件名称和电话可能也是如此。
答案 2 :(得分: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
}
}