检查单选按钮和字段以启用链接

我有一个带有很多单选按钮和几个文本字段的表单。 所以…

     

在打开灯箱的表单后面有一个链接。 我希望只有选中单选按钮并填写两个字段才能启用该链接。 非常感谢你的帮助。

我认为这应该有效:

 if ($('input:radio[name=pic]:checked').length && $('#email').val().length && $('#comment').val().length) { /* activate the lightbox link */ } else { alert("Please ensure you've selected one of the radio buttons, and filled out the text-fields."); } 

编辑修改你的代码:

HTML:

 

jQuery的:

 $('form').submit( function() { if ($('input:radio[name=pic]:checked').length && $('#email').val().length && $('#comment').val().length) { /* activate the lightbox link */ } else { alert("Please ensure you've selected one of the radio buttons, and filled out the text-fields."); } return false; }); 

JS小提琴演示 。

 $("#link").click(function(e) { if ($('input[name=pic]:checked').val() && $('#email').val() && $('#comment').val()) lightbox(); else e.preventDefault(); }); 
 function checklink() { if ($('[name=pic]').val()) && $('#email').val() && $('#comment').val()) { $('#linkId').hide(); } else { $('#linkId').show() } 

然后在输入字段中添加onchange="checklink()"

编辑 :根据@fehergeri评论修复禁用