检查单选按钮和字段以启用链接 [英] check radio buttons and fields to enable link

查看:127
本文介绍了检查单选按钮和字段以启用链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form with a lot of radio buttons and a couple of text fields. So...

<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="">

并且在表单后面会有一个链接,用于打开Lightbox。我希望只有在选择单选按钮并填写了两个字段时才启用链接。非常感谢你的帮助。

and there's a link after the form that will open up a lightbox. I want the link to be enabled only if a radio button is selected and the two fields are filled out. Thanks a lot for your help.

推荐答案

我认为这应该有效:

I think that this should work:

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.");
}



已编辑修改你的代码:

<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>



jQuery:



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 Fiddle演示

这篇关于检查单选按钮和字段以启用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆