0

我正在尝试制作一些项目,我希望在我选择专家按钮时显示一个文本框,而当我单击学习者按钮时不显示文本框....

我已经编写了这段代码,但无法解决问题...请帮助...

<html>
<head>
<script language="javascript"> 

function toggleContent(showHideDiv, switchTextDiv) {
    var text = document.getElementById(showHideDiv);
    var ele = document.getElementById(switchTextDiv);
    var rad_val='';
    for (var i=0; i < document.text.role.length; i++)
    {
        if (document.text.role[i].checked)
        {
            rad_val = document.text.role[i].value;
        }
    }   

    if(rad_val=='learner'){
            ele.style.display = "block";
    }
    else {
        ele.style.display = "none";
    }
}
</script>
</head>
<body>
<table>
  <tr >
    <td  ><option value="1"> 1 </option>
    </td>
    <td> Role </td>
    <form id="form1">
      <td><label>
        <input type="radio" name="role" value='learner' onClick="toggleContent('form1','div1')" >
        Learner </label>
      </td>
      <td><label>
        <input type="radio" name="role" value='expert' onClick="toggleContent('form1','div1')" >
        Expert </label>
    </form>
<td ><div ID="div1" align=right style="display:none;">
        <label class="labell labelUser" >why?</label>
        <textarea name="description" align="right" id="description"  cols="40" rows="5" class="inputbox">Why?</textarea>
        <span id="descriptionError" class="notifyForUser" spanError></span> </div></td>  
  </tr>
  <tr >
    <td  ><option value="2"> 2 </option>
    </td>
    <td> Role </td>
    <form id="form2">
      <td><label>
        <input type="radio" name="role" value='learner' onClick=toggleContent('form2','div2') >
        Learner </label>
      </td>
      <td><label>
        <input type="radio" name="role" value='expert' onClick=toggleContent('form2','div2') >
        Expert </label>
      </td>
    </form>
    <td ><div ID="div2" align=right style="display:none;">
        <label class="labell labelUser" >why?</label>
        <textarea name="description" align="right" id="description"  cols="40" rows="5" class="inputbox">Why?</textarea>
        <span id="descriptionError" class="notifyForUser" spanError></span> </div></td>
  </tr>
</table>
</body>
</html>
4

4 回答 4

0
if(rad_val=='learner'){ 
        ele.style.display = "block"; 
} 
else { 
    ele.style.display = "none"; 
} 

没有值等于学习者,但学习者1或学习者2

试试这个:

function toggleContent(showHideDiv, switchTextDiv) {
    var text = document.getElementById(showHideDiv);
    var ele = document.getElementById(switchTextDiv);
    var rad_val = '';
    var pattern = /\d+/g;
    var a=pattern.exec(showHideDiv);

    for (var i = 0; i < document.forms[showHideDiv].role.length; i++) {
        if (document.forms[showHideDiv].role[i].checked) {
            rad_val = document.forms[showHideDiv].role[i].value;
        }
    }
    if (rad_val == 'learner' + a) {

        ele.style.display = "block";
    }
    else {

        ele.style.display = "none";
    }
} 
于 2012-06-07T06:59:42.953 回答
0

你可以这样做:

<script language="javascript"> 
function toggleContent(showHideDiv, switchTextDiv) {
var text = document.getElementById(showHideDiv);
var ele = document.getElementById(switchTextDiv);
var rad_val='';
var obj=text.getElementsByTagName("input");
for (var i=0; i < obj.length; i++)
{
    if (obj[i].checked)
    {
        rad_val = obj[i].value;
    }
}  
if(rad_val.indexOf("learner")!=-1){
        ele.style.display = "block";
}
else {
    ele.style.display = "none";
}
}
</script>
于 2012-06-07T07:09:49.770 回答
0

您的代码逻辑和检索表单元素的方式存在一些错误。

我建议修改整个逻辑,而不是修复你的代码。

更好的做法是通过代码绑定点击事件,而不是从每个输入标签内联。此外,单击单选按钮后(也可以通过键盘选择触发),您可以确定整个单选组的选定值是单击按钮的值。

这是实现相同效果所需的代码:

window.onload = function() {
    var oDiv = document.getElementById("div2");
    var arrRoleButtons = document.getElementsByName("role");
    for (var i = 0; i < arrRoleButtons.length; i++) {
        var oCurrentRole = arrRoleButtons[i];
        oCurrentRole.onclick = function() {
            oDiv.style.display = (this.value === 'learner') ? "block" : "none"; 
        }
    }
};

有了这个,您不再需要onclick从标签中调用:

<input type="radio" name="role" value='learner' />
<input type="radio" name="role" value='expert' />

现场测试用例

于 2012-06-07T07:15:38.050 回答
0

这就是解决方案。感谢@dragon66 解决了我的问题。

function toggleContent(showHideDiv, switchTextDiv) {
    var text = document.getElementById(showHideDiv);
    var ele = document.getElementById(switchTextDiv);
    var rad_val='';
    for (var i=0; i < text.role.length; i++)
    {
        if (text.role[i].checked)
        {
            rad_val = text.role[i].value;
        }
    }   

    if(rad_val=='learner'){
            ele.style.display = "block";
    }
    else {
        ele.style.display = "none";
    }
}
</script>
</head>
<body>
<table>
  <tr >
    <td  ><option value="1"> 1 </option>
    </td>
    <td> Role </td>
    <form id="form1">
      <td><label>
        <input type="radio" name="role" value='learner' onClick="toggleContent('form1','div1')" >
        Learner </label>
      </td>
      <td><label>
        <input type="radio" name="role" value='expert' onClick="toggleContent('form1','div1')" >
        Expert </label>
    </form>
<td ><div ID="div1" align=right style="display:none;">
        <label class="labell labelUser" >why?</label>
        <textarea name="description" align="right" id="description"  cols="40" rows="5" class="inputbox">Why?</textarea>
        <span id="descriptionError" class="notifyForUser" spanError></span> </div></td>  
  </tr>
  <tr >
    <td  ><option value="2"> 2 </option>
    </td>
    <td> Role </td>
    <form id="form2">
      <td><label>
        <input type="radio" name="role" value='learner' onClick=toggleContent('form2','div2') >
        Learner </label>
      </td>
      <td><label>
        <input type="radio" name="role" value='expert' onClick=toggleContent('form2','div2') >
        Expert </label>
      </td>
    </form>
    <td ><div ID="div2" align=right style="display:none;">
        <label class="labell labelUser" >why?</label>
        <textarea name="description" align="right" id="description"  cols="40" rows="5" class="inputbox">Why?</textarea>
        <span id="descriptionError" class="notifyForUser" spanError></span> </div></td>
  </tr>
</table>
</body>
</html>
于 2012-06-18T15:33:05.363 回答