ラジオボックスの選択に基づいて入力のIDを変更しようとしています。
<input type="text" name="judge"/>
<input type="radio" name="type" value="percent"/>Percent |
<input type="radio" name="type" value="decimal"/>Decimal
<br/>
<button type="button" id="mybutton">Check</button>
<br/><br/>
<div id="output"/>
ラジオボタンのパーセントまたは小数をクリックすると、入力ボックスのIDが変更されます。
//Set the ID of the input box on radio change
$('input[name=type]').change(function() {
if ($(this).val() == "percent") {
$('input[name=judge]').attr('id', 'judge_percent');
} else if ($(this).val() == "decimal") {
$('input[name=judge]').attr('id', 'judge_decimal');
}
});
//IF statement depending on the input ID created from the radio button
$('#mybutton').click(function() {
if ($('input[name=type]').val() == "percent") {
value_percent = parseFloat($('#judge_percent').val());
$('#output').html(value_percent);
} else if ($('input[name=type]').val() == "decimal") {
value_decimal = parseFloat($('#judge_decimal').val());
$('#output').html(value_decimal);
}
});
これは中途半端にしか機能しません。「decimal」をチェックしてボタンをクリックすると、入力IDを読み取っていないかのように「NaN」が表示されます。
編集:正しいJsfiddleは次のとおりです:http://jsfiddle.net/6FbdJ/1/