2013-07-08 25 views
0

我有2套单选按钮,这是我要做到以下几点:。选择不同的单选按钮进入不同的html页面

1)当所有的选择都是假的,显示的验证消息(这是工作,但点击时'重复保存'它会添加一个空行,我该如何防止这一点?)。

2)如果只选择设置1或设置2,则显示保存时的错误消息。 3)。当选择Set 1时,Set 2选择需要根据是或否将用户带到不同的页面。

在此先感谢!

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

回答

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'); 
     }); 
    } 
}); 

如果我理解你的问题,这就是你需要的。你真的想在每个错误上添加<br />吗?我添加了淡入/淡出以避免这种情况。希望它可以。

+0

我有
之后的错误消息,所以它显示在一个单独的行,虽然我喜欢fadeOut。 我会尽力解释我需要这样做。 如果选择无线电台1或无线电台2,则同时显示val1和val2。 如果选择了收音机1并且收音机2不是,只显示val2。 如果选择Radio Set 2并且Radio Set 1不是,则只显示val1。 如果选择了无线电套装1并选择了无线电套装2> attachYes,请前往cnn.com。 如果选择了收音机1并选择收音机2> attachNo,请转至yahoo.com。 – squirc77

+0

@ squirc77,好吧,我明白了。在我的演示中,我将它们添加到html中,所以点击后它们不会很多。你也可以用CSS来设计它们。让我知道你是否需要帮助。 – Sergio

+0

@ squirc77,这是CSS的错误消息的另一种选择http://jsfiddle.net/bkSrD/1/ – Sergio

相关问题