我正在尝试根据单选框的选择来更改输入的 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);
}
});
这只能半途而废。如果我检查“十进制”并单击我的按钮,我会得到“NaN”,就好像它没有读取输入 ID。
编辑:这是正确的Jsfiddle:http: //jsfiddle.net/6FbdJ/1/