gpt4 book ai didi

javascript - 单选按钮清除文本框

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:24 25 4
gpt4 key购买 nike

我有两个单选按钮:单击第一个单选按钮,如果他们开始输入信息,然后改变主意并选择第二个单选按钮,则会出现三个文本框,这不会清除他们输入的文本。 So what I am trying to figure out is if there is a way make it clear the text from those textboxes when a new radio button (of the same group) is chosen.任何帮助是极大的赞赏!

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 demo .

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

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 demo .

最后,一个版本摆脱了内联事件处理的干扰性 JavaScript(onclickonchange 等):

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 demo .

引用资料:

关于javascript - 单选按钮清除文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26537486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com