是这样的,我是想实现,当用户点击“我有帐号”时,就进行id="youzhanghao"内的表单提交判断,当点击“还没有帐号”时就进行id="wuzhanghao"内表单提交判断。
(目前我下面的javasript的代码只能实现其中一个id="wuzhanghao"的提交判断,我不知道在这里面这个radio要先怎样判断的,希望高手指点一下,谢谢了。下面是HTML的代码:<form method="post" name="formUser" id="theForm" action="binds.php" >
<p style=""><span class=""><input type="radio" name="s" id="a" value="0" checked="checked" />
<label for="a">我有帐号</label>
</span>
<span class=""><input type="radio" name="s" id="b" value="1" />
<label for="b">还没有帐号</label></span>
</p>
<div id="youzhanghao">
<ul>
<li>用户名:<input name="username" type="text" class="input" id="username" /></li>
<li>密码:<input name="password" type="password" class="input" id="password" /></li>
</ul>
</div>
<div id="wuzhanghao" style="display:none" class="form_yiyou">
<ul>
<li>用户名:<input class="input" name="username2" type="text" id="username2" value="" size="45" />
<span style="color:#FF0000"> *</span></li>
<li>密码:<input type="password" name="password1" id="password1" value="" />
<span style="color:#FF0000"> *</span></li>
<li>确认密码<input type="password" name="pwd" id="pwd" value="" />
<span style="color:#FF0000"> *</span></li>
<li>邮箱:<input type="text" name="email" class="input" id="email" value="" />
<span style="color:#FF0000"> *</span></li>
</ul>
</div>
<p>
<input type="submit" name="Submit" value="提交"/>
</p>
</form>下面是javascript的主要代码:<script language="javascript">
window.onload = function(){
var form = document.getElementById('theForm');
var input = document.getElementsByTagName('input');
document.getElementById('a').onclick = function(){
if(this.checked)
{
document.getElementById('youzhanghao').style.display = '';
document.getElementById('wuzhanghao').style.display = 'none';
}
}
document.getElementById('b').onclick = function(){
if(this.checked)
{
document.getElementById('youzhanghao').style.display = 'none';
document.getElementById('wuzhanghao').style.display = '';
}
}
document.getElementById('b').click();
}
$().ready(function() {
// validate the comment form when it is submitted
// validate signup form on keyup and submit
$.validator.setDefaults({
submitHandler: function(form) {
form.submit();
}
});
$("#theForm").validate({
rules: {
username2: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
password1: {
required: true,
minlength: 5
},
pwd: {
required: true,
equalTo: "#password1"
}
},
messages: {
username2: {
required: username_empty,
minlength: username_shorter + username_invalid
},
email: {
required: email_empty,
email: email_invalid
},
password1: {
required: password_empty,
minlength: password_shorter
},
pwd: {
required: password_empty,
minlength: password_shorter,
equalTo: confirm_password_invalid
}
}
});
});
</script>
(目前我下面的javasript的代码只能实现其中一个id="wuzhanghao"的提交判断,我不知道在这里面这个radio要先怎样判断的,希望高手指点一下,谢谢了。下面是HTML的代码:<form method="post" name="formUser" id="theForm" action="binds.php" >
<p style=""><span class=""><input type="radio" name="s" id="a" value="0" checked="checked" />
<label for="a">我有帐号</label>
</span>
<span class=""><input type="radio" name="s" id="b" value="1" />
<label for="b">还没有帐号</label></span>
</p>
<div id="youzhanghao">
<ul>
<li>用户名:<input name="username" type="text" class="input" id="username" /></li>
<li>密码:<input name="password" type="password" class="input" id="password" /></li>
</ul>
</div>
<div id="wuzhanghao" style="display:none" class="form_yiyou">
<ul>
<li>用户名:<input class="input" name="username2" type="text" id="username2" value="" size="45" />
<span style="color:#FF0000"> *</span></li>
<li>密码:<input type="password" name="password1" id="password1" value="" />
<span style="color:#FF0000"> *</span></li>
<li>确认密码<input type="password" name="pwd" id="pwd" value="" />
<span style="color:#FF0000"> *</span></li>
<li>邮箱:<input type="text" name="email" class="input" id="email" value="" />
<span style="color:#FF0000"> *</span></li>
</ul>
</div>
<p>
<input type="submit" name="Submit" value="提交"/>
</p>
</form>下面是javascript的主要代码:<script language="javascript">
window.onload = function(){
var form = document.getElementById('theForm');
var input = document.getElementsByTagName('input');
document.getElementById('a').onclick = function(){
if(this.checked)
{
document.getElementById('youzhanghao').style.display = '';
document.getElementById('wuzhanghao').style.display = 'none';
}
}
document.getElementById('b').onclick = function(){
if(this.checked)
{
document.getElementById('youzhanghao').style.display = 'none';
document.getElementById('wuzhanghao').style.display = '';
}
}
document.getElementById('b').click();
}
$().ready(function() {
// validate the comment form when it is submitted
// validate signup form on keyup and submit
$.validator.setDefaults({
submitHandler: function(form) {
form.submit();
}
});
$("#theForm").validate({
rules: {
username2: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
password1: {
required: true,
minlength: 5
},
pwd: {
required: true,
equalTo: "#password1"
}
},
messages: {
username2: {
required: username_empty,
minlength: username_shorter + username_invalid
},
email: {
required: email_empty,
email: email_invalid
},
password1: {
required: password_empty,
minlength: password_shorter
},
pwd: {
required: password_empty,
minlength: password_shorter,
equalTo: confirm_password_invalid
}
}
});
});
</script>
$().ready(function() {
// validate the comment form when it is submitted
// validate signup form on keyup and submit
$.validator.setDefaults({
submitHandler: function(form) {
在这里判断点击的是有账号还是无账号。
form.submit();
}
});
你的那个 validata 只是判断输入的是否合法。
哪知道,现在还是出了问题.
现在就是不管你是在"我没有账号"还是"我有账号"的状态下,只要单击提交的按钮,它都会进行一次表单内全部元素的检验,这样就是你要把"我没有账号"或者"我有账号",全部填完才能提交.晕倒...
高手们,要怎么修改,搞了一天了,还没搞定...下面是相关的代码:
$("#theForm").submit(function(){
var tss=$('input:radio[name="s"]:checked').val();
if(tss==1){
//执行"我没有账号"的判断代码,请见下面.
}
else
{执行"我有帐号"的判断代码,请见下面.
"我没有账号"的判断代码:$("#theForm").validate({
rules: {
username2: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
password1: {
required: true,
minlength: 5
},
pwd: {
required: true,
equalTo: "#password1"
}
},
messages: {
username2: {
required: username_empty,
minlength: username_shorter + username_invalid
},
email: {
required: email_empty,
email: email_invalid
},
password1: {
required: password_empty,
minlength: password_shorter
},
pwd: {
required: password_empty,
minlength: password_shorter,
equalTo: confirm_password_invalid
}
}
});
"我有账号"的判断代码:$("#theForm").validate({
rules: {
username: {
required: true,
minlength: 2
},
password: {
required: true,
minlength: 6
}
},
messages: {
username: {
required: username_empty,
minlength: username_shorter + username_invalid
}
password: {
required: password_empty,
minlength: password_shorter
}
}
});
$("#theForm").validate({
rules: {
username2: {
required: true,
minlength: 2
},
email: {
if(checked=="我有账号"){//以下同理,我的想法,你可以试试看看
required:false,
email:false
}
else{
required: true,
email: true
}
},
password1: {
required: true,
minlength: 5
},
pwd: {
required: true,
equalTo: "#password1"
}
},
messages: {
username2: {
required: username_empty,
minlength: username_shorter + username_invalid
},
email: {
required: email_empty,
email: email_invalid
},
password1: {
required: password_empty,
minlength: password_shorter
},
pwd: {
required: password_empty,
minlength: password_shorter,
equalTo: confirm_password_invalid
}
}
});
username: {
if(tss==0){
required: true,
minlength: 2
}
else
{
required: false
} }
其次,不用那么复杂的,你做三个div,一个id为:woyouzhanghao,一个di为womeizhanghao,然后分别填入两个不同的表单,第三个无内容,id为tijiao
点击我有账号按钮就触发$("#tijiao").html($("#woyouzhanghao").html())
点击我没账号按钮就触发$("#tijiao").html($("#womeizhanghao").html())
当然,你也可以仍然用单选框
$("#s").bind({
click:function(){
if($(this).val()==0){
$("#tijiao").html($("#woyouzhanghao").html())
}else{
$("#tijiao").html($("#womeizhanghao").html())
}
}
})
$("#womeizhanghao").hide()
我想了一下,还是不行.
因为form只有一个的,不能分成两个.
不知道还有没其它好点的方法...
rules: {
username2: {
required: true,
minlength: 2
},
email: {
required:fucntion(){
if(tss=="0"){
return false;
}else{
return true;
}
}
email:false
}
},
password1: {
required: true,
minlength: 5
},
pwd: {
required: true,
equalTo: "#password1"
}
},
messages: {
username2: {
required: username_empty,
minlength: username_shorter + username_invalid
},
email: {
required: email_empty,
email: email_invalid
},
password1: {
required: password_empty,
minlength: password_shorter
},
pwd: {
required: password_empty,
minlength: password_shorter,
equalTo: confirm_password_invalid
}
}
});
这样行不行? 这东西都是有思路在多试试几次
我注意倒这段代码,等同于要把所有表单内的所有元素都要进行一个if else判断才行了.
// validate the comment form when it is submitted
// validate signup form on keyup and submit
$.validator.setDefaults({
submitHandler: function(form) {
form.submit();
}
});
$("#theForm").submit(function(){
var tss=$('input:radio[name="s"]:checked').val(); //获取radio的选择进行判断
if(tss=="0"){
//我有账号的判断
$("#theForm").validate({
onfocusout: false,
onkeyup: false,
onclick: false,
rules: {
username: {
required: true,
minlength: 2
},
password: {
required: true,
minlength: 5
}
},
messages: {
username: {
required: username_empty,
minlength: username_shorter + username_invalid
},
password: {
required: password_empty,
minlength: password_shorter
}
}
});
//ss
}
else
{
//我没有账号的判断
$("#theForm").validate({
onfocusout: false,
onkeyup: false,
onclick: false,
rules: {
username2: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
password1: {
required: true,
minlength: 5
},
pwd: {
required: true,
equalTo: "#password1"
}
},
messages: {
username2: {
required: username_empty,
minlength: username_shorter + username_invalid
},
email: {
required: email_empty,
email: email_invalid
},
password1: {
required: password_empty,
minlength: password_shorter
},
pwd: {
required: password_empty,
minlength: password_shorter,
equalTo: confirm_password_invalid
}
}
});
//ss
}
})
谢谢您了chinaskysun