[英]toggle an input field
i am trying this code: 我正在尝试此代码:
<script type="text/javascript">
$(document).ready(function () {
$('#individual,#company').hide();
$('input[name=radios]:radio:checked').change(function() {
$('#individual').toggle(this.selectedIndex === 0);
$('#company').toggle(this.selectedIndex === 1);
});
});
</script>
<div class="radio-toolbar">
<input type="radio" name="radios">
<input type="radio" name="radios">
</div>
<div id="x">
<form method="post" id="customForm" action="">
<div id="individual">
<input name="individual" type="text" />
</div>
<div id="company">
<input name="company" type="text" />
</div>
</form>
</div>
basically i want to show only an input field. 基本上我只想显示一个输入字段。 if i choose the first radio btn, then, i want display the div individual.
如果我选择第一个广播btn,那么我要显示div个人。 If i choose the second radio, should be showed the company content.
如果我选择第二台收音机,应显示公司内容。
thanks 谢谢
I would rewrite the script so the radio's have different values and use the value as the trigger instead of the === 0 / 1 construction.. 我将重写脚本,以便无线电的值不同,并使用该值作为触发器,而不是=== 0/1构造。
<script type="text/javascript">
$(document).ready(function () {
$('#individual,#company').hide();
$("input[name='radios']").change(function() {
switch ($(this).val()) {
case 'ind':
$('#individual').show();
$('#company').hide();
break;
case 'comp':
$('#individual').hide();
$('#company').show();
break;
}
});
});
</script>
<div class="radio-toolbar">
<input type="radio" name="radios" value="ind">
<input type="radio" name="radios" value="comp">
</div>
<div id="x">
<form method="post" id="customForm" action="">
<div id="individual">
<input name="individual" type="text" />
</div>
<div id="company">
<input name="company" type="text" />
</div>
</form>
</div>
Just added value attributes to the input 刚刚将值属性添加到输入中
<div class="radio-toolbar">
<input type="radio" name="radios" value="i">
<input type="radio" name="radios" value="c">
</div>
<div id="x">
<form method="post" id="customForm" action="">
<div id="individual">
<input name="individual" type="text" />
</div>
<div id="company">
<input name="company" type="text" />
</div>
</form>
</div>
... changed the selector, and compare with the aforementioned values, since the selectedIndex property doesn't exist in radio buttons. ...更改了选择器,并与上述值进行比较,因为selectedIndex属性在单选按钮中不存在。
$(document).ready(function () {
$('#individual,#company').hide();
$('input[name=radios]').change(function() {
$('#individual').toggle($(this).val() === 'i');
$('#company').toggle($(this).val() === 'c');
});
});
You can keep it simple and have the radio buttons trigger the change. 您可以保持简单,并让单选按钮触发更改。
<script type="text/javascript">
$(document).ready(function () {
$('#individual').add('#company').hide();
});
</script>
<div class="radio-toolbar">
<input type="radio" name="radios" onclick="$('#individual').show();$('#company').hide();"> Individual
<input type="radio" name="radios" onclick="$('#company').show();$('#individual').hide();"> Company
</div>
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.