I am trying to retrieve the value of the checked button in a radio button group.
<script type="text/javascript">
function ButtonClicked() {
alert($('input[name=datetype] :checked').val());
return;
}
</script>
<f开发者_开发技巧orm action="test.php">
<input type="radio" value="d1" name="datetype" checked onclick="javascript: ButtonClicked()">Date 1 <br />
<input type="radio" value="d2" name="datetype" onclick="javascript: ButtonClicked()"> Date 2 <br />
</form>
The output is always 'undefined' . I am a beginner to jQuery(and JS) so I may be missing something obvious but looking at tons of examples didn't help.
alert($('input[name=datetype]:checked').val());
try without a space
Remove 'javascript' from onclick.
<input type="radio" value="d1" name="datetype" checked onclick="ButtonClicked()">Date 1 <br />
<input type="radio" value="d2" name="datetype" onclick="ButtonClicked()"> Date 2 <br />
much easyser:
<script type="text/javascript">
function ButtonClicked(value) {
alert(value);
return;
}
</script>
<form action="test.php">
<input type="radio" value="d1" name="datetype" checked onclick="javascript:ButtonClicked(this.value)">Date 1 <br />
<input type="radio" value="d2" name="datetype" onclick="javascript: ButtonClicked(this.value)"> Date 2 <br />
</form>
精彩评论