问题
I have 2 sets of radio buttons which I want to do the following:
1) When all selections are false, show validation messages.(This is working but when clicking 'Save' repeatedly it adds a blank row, how do I prevent this?).
2) If just Set 1 or Set 2 is selected show error message for the other on Save.
3). When Set 1 is selected, Set 2 selections need to take user to different pages depending on Yes or No.
Thanks in advance!!
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
<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
<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
Try this:
$('#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');
});
}
});
This does what you need, if I understood your question right. Do you really want to add <br />
on every error? I added a fade in/out to avoid that. Hope its ok.
来源:https://stackoverflow.com/questions/17533957/selecting-different-radio-buttons-goes-to-different-html-pages