javascript 使用单选按钮更改表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6968080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:30:35  来源:igfitidea点击:

change form using radio button

javascripthtml

提问by Sara

hi Guys I am newbie at html and javascript so I need help for a very basic task.

嗨,伙计们,我是 html 和 javascript 的新手,所以我需要帮助完成一项非常基本的任务。

Using radio button I want to change my form for example if 'A' radio button is selected it should show me one form where I can enter my name and age and if 'B' is selected it show show me other form where i can see a picture but text field for name and age are no more visible when I click radio button 'B'

使用单选按钮我想更改我的表单,例如,如果选择了“A”单选按钮,它应该向我显示一个表单,我可以在其中输入我的姓名和年龄,如果选择了“B”,它会向我显示其他表单,我可以看到一张图片,但当我单击单选按钮“B”时,姓名和年龄的文本字段不再可见

any help would be appreciated

任何帮助,将不胜感激

采纳答案by vivek_jonam

i modified the above in a more easy way,, try editing it according to ur needs,,

我以更简单的方式修改了上面的内容,尝试根据您的需要进行编辑,,

<html>
<head>
<script language="Javascript">
function hideA()
{

    document.getElementById("A").style.visibility="hidden";
    document.getElementById("B").style.visibility="visible";    

}

function hideB()
{
    document.getElementById("B").style.visibility="hidden";
    document.getElementById("A").style.visibility="visible";

}
</script>
</head>
<body>Show : 
<input type="radio"  name="aorb" onClick="hideB()" checked>A | 
<input type="radio"  name="aorb" onClick="hideA()">B
<div style="position: absolute; left: 10px; top: 100px;" id="A"><br/>A's text</div>
<div style="position: absolute; left: 10px; top: 100px; visibility:hidden" id="B"><br/>B's text</div>
</body>
</html>

回答by Vinayak Garg

You can use the onchange attribute of an input to call a javascript function, which hides A & shows B or vice versa

您可以使用输入的 onchange 属性来调用 javascript 函数,该函数隐藏 A & 显示 B,反之亦然

 function hideA(x) {
   if (x.checked) {
     document.getElementById("A").style.visibility = "hidden";
     document.getElementById("B").style.visibility = "visible";
   }
 }

 function hideB(x) {
   if (x.checked) {
     document.getElementById("B").style.visibility = "hidden";
     document.getElementById("A").style.visibility = "visible";
   }
 }
Show :
<input type="radio" onchange="hideB(this)" name="aorb" checked>A |
<input type="radio" onchange="hideA(this)" name="aorb">B
<div id="A">
  <br/>A's text</div>
<div id="B" style="visibility:hidden">
  <br/>B's text</div>

回答by CrandellWS

I liked the Answer from Vinayak Garg

我喜欢Vinayak Garg回答

Though a more portable solution was desired that could be used for many options using a basic structure minimal javascript is needed to swap options

虽然需要一个更便携的解决方案,它可以用于使用基本结构的许多选项,但需要最少的 javascript 来交换选项

The example function used in the next 2 snippets is:

在接下来的 2 个片段中使用的示例函数是:

function swapConfig(x) {
  var radioName = document.getElementsByName(x.name);
  for (i = 0; i < radioName.length; i++) {
    document.getElementById(radioName[i].id.concat("Settings")).style.display = "none";
  }
  document.getElementById(x.id.concat("Settings")).style.display = "initial";
}

function swapConfig(x) {
    var radioName = document.getElementsByName(x.name);
    for(i = 0 ; i < radioName.length; i++){
      document.getElementById(radioName[i].id.concat("Settings")).style.display="none";
    }
    document.getElementById(x.id.concat("Settings")).style.display="initial";
  }
<fieldset>
  <legend>Url and Domain Configuration</legend>
  <p>
    <label for="production">Production</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="production" checked="checked" />
    <label for="development">Development</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="development" />
  </p>
  <div id="productionSettings">
    <br/>Production Settings
    <p>
      <label for="p1">Production1</label>
      <input type="text" name="p1" value="/">
    </p>
  </div>
  <div id="developmentSettings" style="display:none">
    <br/>Development Settings
    <p>
      <label for="d1">Developent1</label>
      <input type="text" name="d1" value="/">
    </p>
  </div>
</fieldset>

Doing it this way you can add new options without changing the javascript such as adding alphaand betaoptions as shown below you will see the same javascript is used.

通过这种方式,您可以添加新选项而无需更改 javascript,例如添加alphabeta选项,如下所示,您将看到使用了相同的 javascript。

function swapConfig(x) {
  var radioName = document.getElementsByName(x.name);
  for (i = 0; i < radioName.length; i++) {
    document.getElementById(radioName[i].id.concat("Settings")).style.display = "none";
  }
  document.getElementById(x.id.concat("Settings")).style.display = "initial";
}
<fieldset>
  <legend>Url and Domain Configuration</legend>
  <p>
    <label for="production">Production</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="production" checked="checked" />
    <label for="development">Development</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="development" />
    <label for="alpha">Alpha</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="alpha" />
    <label for="beta">Beta</label>
    <input type="radio" onchange="swapConfig(this)" name="urlOptions" id="beta" />
  </p>
  <div id="productionSettings">
    <br/>Production Settings
    <p>
      <label for="p1">Production</label>
      <input type="text" name="p1" value="/">
      </p>
  </div>
  <div id="developmentSettings" style="display:none">
    <br/>Development Settings
    <p>
      <label for="d1">Developent</label>
      <input type="text" name="d1" value="/">
    </p>
  </div>
  <div id="alphaSettings" style="display:none">
    <br/>Alpha Settings
    <p>
      <label for="a1">Alpha</label>
      <input type="text" name="a1" value="/">
    </p>
  </div>
  <div id="betaSettings" style="display:none">
    <br/>Beta Settings
    <p>
      <label for="b1">Beta</label>
      <input type="text" name="b1" value="/">
      </p>
  </div>
</fieldset>

It could be even more reusable by adding a second variable to the function:

通过向函数添加第二个变量,它可以更加重用:

function swapConfig(x, y) {
  var radioName = document.getElementsByName(x.name);
  for (i = 0; i < radioName.length; i++) {
    document.getElementById(radioName[i].id.concat(y)).style.display = "none";
  }
  document.getElementById(x.id.concat(y)).style.display = "initial";
}

function swapConfig(x, y) {
      var radioName = document.getElementsByName(x.name);
      for (i = 0; i < radioName.length; i++) {
        document.getElementById(radioName[i].id.concat(y)).style.display = "none";
      }
      document.getElementById(x.id.concat(y)).style.display = "initial";
    }
<fieldset>
  <legend>Url and Domain Configuration</legend>
  <p>
    <label for="production">Production</label>
    <input type="radio" onchange="swapConfig(this, 'Settings')" name="urlOptions" id="production" checked="checked" />
    <label for="development">Development</label>
    <input type="radio" onchange="swapConfig(this,'Settings')" name="urlOptions" id="development" />
    <label for="alpha">Alpha</label>
    <input type="radio" onchange="swapConfig(this,'Settings')" name="urlOptions" id="alpha" />
    <label for="beta">Beta</label>
    <input type="radio" onchange="swapConfig(this,'Settings')" name="urlOptions" id="beta" />
  </p>
  <p>
    <label for="alphaVar">Alpha</label>
    <input type="radio" onchange="swapConfig(this,'Val')" name="urlVars" id="alphaVar" checked="checked"  />
    <label for="betaVar">Beta</label>
    <input type="radio" onchange="swapConfig(this,'Val')" name="urlVars" id="betaVar" />
  </p>
  <div id="productionSettings">
    <br/>Production Settings
    <p>
      <label for="p1">Production</label>
      <input type="text" name="p1" value="/">
      </p>
  </div>
  <div id="developmentSettings" style="display:none">
    <br/>Development Settings
    <p>
      <label for="d1">Developent</label>
      <input type="text" name="d1" value="/">
    </p>
  </div>
  <div id="alphaSettings" style="display:none">
    <br/>Alpha Settings
    <p>
      <label for="a1">Alpha</label>
      <input type="text" name="a1" value="/">
    </p>
  </div>
  <div id="betaSettings" style="display:none">
    <br/>Beta Settings
    <p>
      <label for="d1">Beta</label>
      <input type="text" name="b1" value="/">
      </p>
  </div>
  <div id="alphaVarVal">
    <br/>Alpha Values
    <p>
      <label for="aV1">Alpha Vals</label>
      <input type="text" name="aV1" value="/">
      </p>
  </div>
  <div id="betaVarVal" style="display:none">
    <br/>Beta Values
    <p>
      <label for="bV1">Beta Vals</label>
      <input type="text" name="bV1" value="/">
      </p>
  </div>
</fieldset>

Javascript for loops are well described in this Answer of the question For-each over an array in JavaScript?

Javascript for 循环在问题For-each over an array in JavaScript?

回答by Syed Ali

    <!DOCTYPE html>
    <html>
    <body>

    <div class="row clearfix">

      <input type="radio" name="salary_status" id="Hourly"  onclick="Hourly()"> &nbsp; Hourly &nbsp;

   <input type="radio" name="salary_status" id="Percentage"  onclick="Percentage()">&nbsp; Percentage&nbsp;

                            </div>
                                <div class="row clearfix">
                                <p id="hourly" style="display:none"> <input type="text" placeholder=" Hourly" name="Hourly" placeholder="Hourly" required="required" class="form-control col-md-9 col-xs-12" ></p>
                                <p id="percentage" style="display:none"> <input type="number"  name="Percentage"  placeholder="Percentage" required="required" class="form-control col-md-9 col-xs-12" ></p>


                            </div>

    <script>
     var text1 = document.getElementById("percentage");
     var text = document.getElementById("hourly");
    function Hourly() {
        var checkBox = document.getElementById("Hourly");

        if (checkBox.checked == true){
            text.style.display = "block";
            text1.style.display = "none";
        } else{
           text.style.display = "none";
        }
    }
    function Percentage() {
        var checkBox = document.getElementById("Percentage");

        if (checkBox.checked == true){
            text1.style.display = "block";
            text.style.display = "none";
        } else {
           text.style.display = "none";
        }
    }
    </script>

    </body>
    </html>

回答by Srikanth Venkatesh

This can be done like this.

这可以像这样完成。

    <html>
    <head>
          <script language="Javascript">
               function show(x)
                              {
                                var element=document.getElementById(x.id);
                                if(element.id=='a')
                                {
                                  document.getElementById("area").innerHTML="Name : <input type='text' id='name' >";
                                }
                                else 
                                {
                                  document.getElementById("area").innerHTML="<img src='imgSrc' alt='NoImage'>"
                                }
                              }
           </script>
   </head>
   <body> 
        <input type="radio" onclick="show(this)" name="radioButton"  id="a" >A  
        <input type="radio" onclick="show(this)" name="radioButton"  id="b" >B
        <br> <br> <br>
    <div id="area">  </div>
   </body>
   </html>