我有这个几乎可以工作的位: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 网站上工作。任何检查的想法也将不胜感激。