2015-04-28 93 views
0

我有这个js(jQuery的):jQuery来隐藏虚拟按键和显示提交按钮

<script type="text/javascript">  
    if ($('input.checkbox_check').is(':checked')) { 
     $('#hide-temp-submit').hide(); 
     $('#show-submit').show(); 
    } 
</script> 

而这个HTML:

<input type="checkbox" class="checkbox_check" name="" value=""> 
<button class="login_button" id='hide-temp-submit' style="background-color:#101010;color:grey;">Please Agree to Terms</button> 
<button class="login_button" id='create_button show-submit' style="display:none">Create Account</button> 

上隐藏的 '虚拟' 的提交按钮任何帮助,并显示复选框被选中后的实际提交按钮?

谢谢!

+0

是否临时按钮执行任何功能?如果没有,为什么不标记你的复选框?然后,您可以选中表单提交,如果复选框被选中或者默认禁用了真实按钮,则在复选框被选中时启用它。 – Jack

回答

1

使用类 - ......你有你的第二个按钮ID的空间:

<script type="text/javascript">  
    if ($('input.checkbox_check').is(':checked')) { 
     $('.hide-temp-submit').hide(); 
     $('.show-submit').show(); 
    } 
</script> 

HTML:

<input type="checkbox" class="checkbox_check" name="" value=""> 
<button class="login_button hide-temp-submit" style="background-color:#101010;color:grey;">Please Agree to Terms</button> 
<button class="login_button create_button show-submit" style="display:none">Create Account</button> 
1

您的代码大多是正确的,但是你没有叫在特定事件上,仅在页面加载时。

您只需将事件连接到复选框更改,如上所述,您的ID对提交按钮无效。

$(".checkbox_check").on("change", function() { 
 
    if ($('input.checkbox_check').is(':checked')) { 
 
    $('#hide-temp-submit').hide(); 
 
    $('#show-submit').show(); 
 
    } else { 
 
    $('#hide-temp-submit').show(); 
 
    $('#show-submit').hide();  
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="checkbox" class="checkbox_check" name="" value=""> 
 
<button class="login_button" id='hide-temp-submit' style="background-color:#101010;color:grey;">Please Agree to Terms</button> 
 
<button class="login_button" id='show-submit' style="display:none">Create Account</button>

0

时页面加载,你需要一个事件处理程序来管理用户输入您的代码将只运行

$(function(){  
    $('input.checkbox_check').change(function(){ 
     $('#hide-temp-submit').toggle(!this.checked); 
     $('#show-submit').toggle(this.checked); 
    }).change(); // trigger event on page load (if applicable) 
}); 

toggle(boolean)会隐藏/显示

注意:在show-submit按钮上有一个无效的空格分隔ID

DEMO