Javascript 选中单选按钮和字段以启用链接

Javascript 选中单选按钮和字段以启用链接,javascript,jquery,validation,forms,Javascript,Jquery,Validation,Forms,我有一个表单,有很多单选按钮和几个文本字段。所以 <input type="radio" name="pic" id="pic" value="val1"> <input type="radio" name="pic" id="pic" value="val2"> <input type="radio" name="pic" id="pic" value="val3"> <input type="text" name="email" id="email

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

<input type="radio" name="pic" id="pic" value="val1">
<input type="radio" name="pic" id="pic" value="val2">
<input type="radio" name="pic" id="pic" value="val3">

<input type="text" name="email" id="email" value="">
<input type="text" name="comment" id="comment" value="">


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

我认为这应该有效:

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:

然后将
onchange=“checklink()”
添加到输入字段中


编辑:修复了根据@fehergeri注释禁用的问题

您不能有多个元素具有相同的
id
(这是无效的(x)html,
id
在文档中必须是唯一的)。改用
。加上@David,我想这会帮助你:这有点复杂,不是吗?@fehergeri,是的。嗯,也许…很晚了,我累了。。?我现在应该停止说话了,不是吗?
<form action="#" method="post">
    <input type="radio" name="pic" id="pic" value="val1">
    <input type="radio" name="pic" id="pic" value="val2">
    <input type="radio" name="pic" id="pic" value="val3">

    <input type="text" name="email" id="email" value="">
    <input type="text" name="comment" id="comment" value="">
    <input type="submit" value="submit" />
</form>
$('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;
});
$("#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()
}