单选按钮可清除文本框

Radio button clears text boxes

本文关键字:文本 清除 单选按钮      更新时间:2023-09-26

我有两个单选按钮:单击第一个单选按钮,如果他们开始输入信息,就会出现三个文本框,然后改变主意,选择第二个单选按钮——它不会清除他们输入的文本。因此,我想弄清楚的是,当选择(同一组的)新单选按钮时,是否有办法清除这些文本框中的文本。非常感谢您的帮助!

http://jsfiddle.net/k0paz2pj/

    <input 
      type="radio" 
      value="Yes" 
      name="lien" 
      id="lien" 
      onchange="showhideForm(this.value);"/><label for="lien">Lien</label>
   <input 
     type="radio" 
     value="None" 
     name="lien" 
     id="nolien" 
     onchange="showhideForm(this.value);"/><label for="nolien">No Lien</label>
    <div id="div1" style="display:none">
    <div class="clearfix">
           <p>
                <label for="lname">Lienholder Name:</label>
                  <input 
                    type="text" 
                    name="lienlname" 
                    id="lienlname">
                </p>
                <p>
                <label for="laddress">Lienholder Address:</label>
                  <input 
                    type="text" 
                    name="lienladdress"  
                    id="lienladdress">
                </p>
                <p>
                <label for="ldate">Date of Lien:</label>
                  <input 
                    type="text" 
                    name="lienldate" 
                    id="datepicker2">
                </p>
               </div>
    </div>
               <div id="div2" style="display:none">
    <!---You are not qualified to see this form.--->
    </div>
   <br>
    <script type="text/javascript">
function showhideForm(lien) {
    if (lien == "Yes") {
        document.getElementById("div1").style.display = 'block';
        document.getElementById("div2").style.display = 'none';
    } 
   else if (lien == "None") {
        document.getElementById("div2").style.display = 'block';
        document.getElementById("div1").style.display = 'none';
    }
}
</script>

一种方法,使用问题/JS Fiddle演示中的纯JavaScript:

function showhideForm(lien) {
    if (lien == "Yes") {
        document.getElementById("div1").style.display = 'block';
        document.getElementById("div2").style.display = 'none';
    } else if (lien == "None") {
        document.getElementById("div2").style.display = 'block';
        document.getElementById("div1").style.display = 'none';
        // getting all the input elements within '#div1' (using a CSS selector):
        var inputs = document.querySelectorAll('#div1 input');
        // iterating over those elements, using Array.prototype.forEach,
        // and setting the value to '' (clearing them):
        [].forEach.call(inputs, function (input) {
            input.value = '';
        });
    }
}

JS Fiddle演示。

上面的一种稍微简洁一点的形式(或者,如果不是更简洁,重复更少):

function showhideForm(lien) {    
    var isYes = lien.trim().toLowerCase() === 'yes',
        div1 = document.getElementById('div1'),
        div2 = document.getElementById('div2');
    div1.style.display = isYes ? 'block' : 'none';
    div2.style.display = isYes ? 'none' : 'block';
    if (!isYes) {
        [].forEach.call(div1.getElementsByTagName('input'), function (input) {
            input.value = '';
        });
    }
}

JS Fiddle演示。

最后,还有一个版本,它摆脱了内嵌事件处理(onclickonchange等)的突兀JavaScript:

function showhideForm() {
    // 'this' in the function is the radio-element to which
    // the function is bound as an event-handler: 
    var isYes = this.value.trim().toLowerCase() === 'yes',
        div1 = document.getElementById('div1'),
        div2 = document.getElementById('div2');
    div1.style.display = isYes ? 'block' : 'none';
    div2.style.display = isYes ? 'none' : 'block';
    if (!isYes) {
        [].forEach.call(div1.getElementsByTagName('input'), function (input) {
            input.value = '';
        });
    }
}
// finding the elements with the name of 'lien':
var lienRadios = document.getElementsByName('lien');
// iterating over those elements, using forEach (again):
[].forEach.call(lienRadios, function (lien) {
    // adding a listener for the 'change' event, when it
    // occurs the showhideForm function is called:
    lien.addEventListener('change', showhideForm);
});

JS Fiddle演示。

参考文献:

  • Array.prototype.forEach()
  • document.getElementsByTagName()
  • document.getElementsByName()
  • document.querySelectorAll()
  • EventTarget.addEventListener()
  • Function.prototype.call()
  • String.prototype.toLowerCase()
  • CCD_ 10

当检查另一个radio时,您可以始终使用此选项:

$("#div1 .clearfix input:text").val("");

function showhideForm(lien) {
    if (lien == "Yes") {
        document.getElementById("div1").style.display = 'block';
        document.getElementById("div2").style.display = 'none';
    } 
   else if (lien == "None") {
        document.getElementById("div2").style.display = 'block';
        document.getElementById("div1").style.display = 'none';
        $("#div1 .clearfix input:text").val("");//here use to clear inputs
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" value="Yes" name="lien" id="lien" onchange="showhideForm(this.value);"/><label for="lien">Lien</label>
&nbsp;&nbsp;&nbsp;<input type="radio" value="None" name="lien" id="nolien" onchange="showhideForm(this.value);"/><label for="nolien">No Lien</label>
<div id="div1" style="display:none">
<div class="clearfix">
       <p>
            <label for="lname">Lienholder Name:</label>
            <input type="text" name="lienlname" id="lienlname">
            </p>
            <p>
            <label for="laddress">Lienholder Address:</label>
            <input type="text" name="lienladdress"  id="lienladdress">
            </p>
            <p>
            <label for="ldate">Date of Lien:</label>
            <input type="text" name="lienldate" id="datepicker2">
            </p>
           </div>
</div>
           <div id="div2" style="display:none">
<!---You are not qualified to see this form.--->
</div>

在(讨厌)评论(开玩笑)后,js方法:

function showhideForm(lien) {
  if (lien == "Yes") {
    document.getElementById("div1").style.display = 'block';
    document.getElementById("div2").style.display = 'none';
  } else if (lien == "None") {
    document.getElementById("div2").style.display = 'block';
    document.getElementById("div1").style.display = 'none';
    //js
    container = document.getElementById('div1');
    inputs = container.getElementsByTagName('input');
    for (index = 0; index < inputs.length; ++index) {
      inputs[index].value = "";
    }
  }
}
<input type="radio" value="Yes" name="lien" id="lien" onchange="showhideForm(this.value);" />
<label for="lien">Lien</label>
&nbsp;&nbsp;&nbsp;
<input type="radio" value="None" name="lien" id="nolien" onchange="showhideForm(this.value);" />
<label for="nolien">No Lien</label>
<div id="div1" style="display:none">
  <div class="clearfix">
    <p>
      <label for="lname">Lienholder Name:</label>
      <input type="text" name="lienlname" id="lienlname">
    </p>
    <p>
      <label for="laddress">Lienholder Address:</label>
      <input type="text" name="lienladdress" id="lienladdress">
    </p>
    <p>
      <label for="ldate">Date of Lien:</label>
      <input type="text" name="lienldate" id="datepicker2">
    </p>
  </div>
</div>
<div id="div2" style="display:none">
  <!---You are not qualified to see this form.--->
</div>