【问题标题】:Selecting different radio buttons goes to different html pages选择不同的单选按钮转到不同的 html 页面
【发布时间】:2013-07-06 05:13:09
【问题描述】:

我有 2 组单选按钮,我想执行以下操作:

1) 当所有选择都为 false 时,显示验证消息。(这是有效的,但是当反复单击“保存”时会添加一个空白行,我该如何防止这种情况发生?)。

2) 如果只选择了 Set 1 或 Set 2,则在保存时显示另一个错误消息。

3)。选择 Set 1 时,Set 2 选择需要根据 Yes 或 No 将用户带到不同的页面。

提前致谢!!

Link to jsfiddle

<span id="val1" style="display:none; color:red;"> Please make a selection</span> 
<strong>Radio Set 3</strong>

<input type="radio" name="radio" id="attachYes" />Yes &nbsp;
<input type="radio" name="radio" id="attachNo" />
<label for="radio2">No</label>
<p> 
    <span id="val2" style="display:none; color:red;"> Please make a selection</span><strong>Radio Set 2</strong>
    <input type="radio" name="radio2" id="NotifYes" />Yes &nbsp;
    <input type="radio" name="radio2" id="NotifNo" />No
</p>
<p>
    <a href="#" id="Qbtn">Save</a>
</p>

JS

$('#Qbtn').click(function () {
    if ((!$('#attachYes').attr('checked') === true) && (!$('#attachNo').attr('checked') === true) && (!$('#NotifYes').attr('checked') === true) && (!$('#NotifNo').attr('checked')) === true) {
        //the validations are displaying correctly
        $('#val1').show().append('<br>');
        $('#val2').show().append('<br>');
    } else {
        if (($('#attachYes').attr('checked') === true) || ($('#attachNo').attr('checked') === true) && (!$('#NotifNo').attr('checked') === true) && ($('#NotifYes').attr('checked')) === true) {
            //the user should be taken to page1.html when clicking save and it isn't doing it                  
            window.location = "page1.html";

            //then how do I write if either of radio set 1 is true and #NotifNo of Radio set 2 is true go to page2.html.                
        }
    }
});

【问题讨论】:

    标签: jquery radio


    【解决方案1】:

    Demo here

    试试这个:

    $('#Qbtn').click(function () {
        if ($('#attachYes').prop('checked') && $('#NotifYes').prop('checked')) {
            //go to http://www.yahoo.com         
            window.location = "http://www.yahoo.com";
        } else if ($('#attachYes').prop('checked') && $('#NotifNo').prop('checked')) { 
            //go to http://www.cnn.com                  
            window.location = "http://www.cnn.com";
        } else {
            //the validations are displaying correctly
            $('#val1, #val2').fadeOut('fast', function () {
                $(this).fadeIn('slow');
            });
        }
    });
    

    如果我正确理解您的问题,这可以满足您的需求。您真的想在每个错误上添加&lt;br /&gt; 吗?我添加了淡入/淡出以避免这种情况。希望没事。

    【讨论】:

    • 我在错误消息之后有
      ,因此它显示在单独的行上,尽管我喜欢淡出。我将尝试更多地解释我需要这样做。如果未选择 Radio Set 1 或 Radio Set 2,则同时显示 val1 和 val2。如果选择了 Radio Set 1 而未选择 Radio Set 2,则仅显示 val2。如果选择了 Radio Set 2 而未选择 Radio Set 1,则仅显示 val1。如果选择了 Radio Set 1 并选择了 Radio Set 2 > attachYes,请访问 cnn.com。如果 Radio Set 1 被选中并且 Radio Set 2 > attachNo 被选中去yahoo.com。
    • @squirc77,好的,我明白了。在我的演示中,我将它们添加到了 html 中,因此单击后它们不会很多。您还可以使用 CSS 设置它们的样式。如果您需要帮助,请告诉我。
    • @squirc77,这里是带有 CSS jsfiddle.net/bkSrD/1的错误消息的另一个选项
    • 不幸的是我还没有弄清楚。我需要 5 个条件语句来执行此操作,但我写得不够熟练。1)如果未选择 Radio Set 1 或 Radio Set 2,则同时显示 val1 和 val2。 2) 如果选择了 Radio Set 1 而未选择 Radio Set 2,则仅显示 val2。 3) 如果选择了 Radio Set 2 而未选择 Radio Set 1,则仅显示 val1。 4) 如果选择了 Radio Set 1 并且选择了 Radio Set 2 > attachYes,请访问 cnn.com。 5) 如果 Radio Set 1 被选中并且 Radio Set 2 > attachNo 被选中去yahoo.com。
    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 2011-07-10
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2014-04-13
    • 2014-12-15
    • 1970-01-01
    相关资源
    最近更新 更多