用表格输入改变无线电值

时间:2017-01-27 16:20:18

标签: javascript jquery html

我正在尝试使用表单输入更改单选按钮的值:https://jsfiddle.net/91oz4nfL/

基本上默认情况下我希望它为NO但如果参数被触发,则将单选按钮更改为YES

function calculateBMI() {

  var weight = document.getElementById('input1').value;
  var height = document.getElementById('input2').value / 100;
  var output = document.getElementById('output');

  var bmi = ((weight) / (height * height));

  var result = ""
  if (bmi > 22) {
    result = "Change question radio to yes!";
  }

  bmi = bmi.toFixed(2);

  if (bmi && bmi != Number.POSITIVE_INFINITY) {
    output.innerHTML = result;
    output.style.display = 'block';
  } else {
    result.innerHTML = '';
    output.style.display = 'none';
  }
}

document.getElementById('input1').addEventListener('keyup', calculateBMI);
document.getElementById('input2').addEventListener('keyup', calculateBMI);

2 个答案:

答案 0 :(得分:2)

您只需在if语句中添加几行,就像这样

if (bmi > 22) {
    document.getElementById("Radio9").checked = true;
    result = "Change question radio to yes!";
} else {
    document.getElementById("Radio10").checked = true;
}

如果单选按钮为true,则表示已选中。上面的代码设置“是”单选按钮以检查bmi是否大于22,否则将“否”单选按钮设置为true。

https://jsfiddle.net/91oz4nfL/

答案 1 :(得分:1)

试试这个:

  var radioYes = document.getElementById('Radio9');
  var radioNo = document.getElementById('Radio10');
  var weight = document.getElementById('input1').value;
  var height = document.getElementById('input2').value / 100;
  var output = document.getElementById('output');

  var bmi = ((weight) / (height * height));

  var result = ""
  debugger
  if (bmi > 22) {
    result = "Change question radio to yes!";
    radioYes.checked = true;
  }else{
    radioNo.checked = false;
  }

https://jsfiddle.net/n820pz3e/1/