我有这个几乎工作位: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");
}
});
});
我不能让它完全工作。当用户输入其中一个字段(如您的姓名)时,提交按钮不再被禁用。
我想保持Submit按钮禁用,直到满足所有3个'required'字段。我认为代码是非常清楚的-但它仍然不能工作。
此外,它在JSFiddle中工作,但在我试图让它工作的Wordpress网站上不工作。
试着切换手到ors。小心你的逻辑,试着大声说出来,看看它是否有意义:
Email为空,name为空,phone为空,则验证为假。您实际想要的是说,如果任何字段为空,则验证为假。
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) {
从我所看到的,我要做的第一件事来调试这个(代码有点混乱,所以不是复制它,我会给你一些指导方针)。
代替
if(validation = true )
试
if(validation)
这是因为在没有任何参数的情况下,它将检查是否为false、undefined或null。同样的方法也可以用于电子邮件名和电话。
所以基本上:
<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
}
}