I am trying to load a new page (a n开发者_开发知识库ew div actually using jQuery mobile) depending on what radio button is selected.
<div id="main_menu">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<div class="radioOne">
<input type="radio" name="entry" id="radio-choice-1" value="addEmail" checked="checked" />
<label for="radio-choice-1" class="boldWhite">Add Email Address</label>
</div>
<div id="entry_active_contests">
Active Contest(s)
</div>
<input type="radio" name="entry" id="radio-choice-2" value="contest1" />
<label for="radio-choice-2" class="boldWhite">Contest</label>
</fieldset>
</div>
</div>
The script is like
$('.choose').click(function(){
if ($("input[@name='entry']:checked").val() == 'addEmail'){
window.location.hash ="first";
alert ('1');
}
else if ($("input[@name='entry']:checked").val() == 'contest1') {
window.location.hash ="contest";
alert ('2');
} else {
alert ('3');
}
});
All I ever get is alert 3. It is extra weird because I made a fiddle and it works fine. http://jsfiddle.net/zkQHP/1/
Any ideas of why this wouldnt work for my actual site?
This works fine in JSFiddle http://jsfiddle.net/kasdega/QMw9P/ I agree with epascarello's comment I'd alter the code to read:
$('.choose').click(function() {
var valToCompare = $("input[name='entry']:checked").val();
if (valToCompare == 'addEmail') {
window.location.hash = "first";
alert('1');
} else if (valToCompare == 'contest1') {
window.location.hash = "contest";
alert('2');
} else {
alert('3');
}
});
Do you need this
else {
alert ('3');
}
Why not make it two if statements or an if/else statement?
$('.choose').click(function(){
var val = $("input[@name='entry']:checked").val();
if(val == 'addEmail'){
window.location.hash ="first";
alert ('1');
}
if(val == 'contest1') {
window.location.hash ="contest";
alert ('2');
}
});
Works here: http://jsfiddle.net/jasongennaro/zkQHP/6/
You can easily grab the selected radio button with $(':checked')
. I'm not sure why you're jumping through these hoops.
$('.choose').click(function(){
console.log($(':checked').val());
// You have the selected radio button here
// Do stuff
});
精彩评论