0

没有查询!当用户选择有一个婴儿或军事离开单选按钮弹出。我无法获取所选按钮的值并将带有 id="choice" 的 innerHTML 更改为该值。有人可以帮忙吗?

http://jsfiddle.net/73Vq9/

<html>
<head>
<script>
//this function deals with changing the event
function changeMessage(oElement) {
    //have a baby
    if (oElement.value == "100") {
        document.getElementById("btn").style.display = "none";
        document.getElementById("radio").style.display = "inline-block";
        document.getElementById("rd1").innerHTML = "C-Section";
        document.getElementById("rd2").innerHTML = "Regular Birth";
        document.getElementById("rd1").value = "C-Section";
        document.getElementById("rd2").value = "Regular Birth";
    //military leave
    } else if (oElement.value == "15") {
        document.getElementById("btn").style.display = "none";
        document.getElementById("radio").style.display = "inline-block";
        document.getElementById("rd1").innerHTML = "Training";
        document.getElementById("rd2").innerHTML = "Active Duty";
        document.getElementById("rd1").value = "Training";
        document.getElementById("rd2").value = "Active Duty";
    //no value, the first, blanck value
    } else if (oElement.value == "0") {
        document.getElementById("btn").style.display = "none";
        document.getElementById("radio").style.display = "none";

    } else {
        document.getElementById("btn").style.display = "inline-block";
        document.getElementById("radio").style.display = "none";
    }
    return;
}
</script>
</head>
<body>


<div style="margin-left:250px;">
<select id="leave" onchange="changeMessage(this);">
  <option value="0"></option>
  <option value="5">Get Married</option>
  <option value="100">Have a Baby</option>
  <option value="90">Adopt a Child</option>
  <option value="35">Retire</option>
  <option value="15">Military Leave</option>
  <option value="25">Medical Leave</option>
</select>
 <button id="btn" style="display:none" onclick="getInfo()"type="button">Process</button>
</div>
<div id="radio" style="display:none">
    <div id="rd1"></div><input type="radio" name="sex" value="" />
    <div id="rd2"></div><input type="radio" name="sex" value=""/>
</div>
    <div id="choice">Radio Choice</div>
4

1 回答 1

2

您的input值永远不会设置,因此将始终为空。

当您在上一个问题https://stackoverflow.com/questions/12091968/javascript-function-not-chang-input-types中更改了代码时,您将其id从a 移到了inputa <div>,因此您的 JavaScript 不再设置输入的值。

您需要更改 JavaScript 以正确设置输入,然后是:

HTML

<input type="radio" name="sex" value="" onclick="showChoice(this)"/>

JavaScript

function showChoice(input) {
    document.getElementById('choice').innerHTML = "Radio Choice=" + input.value;
}

应将所选输入的值附加到<div id="choice">

于 2012-08-23T13:25:24.360 回答