文本框的基础上的复选框状态切换的知名度-jQuery [英] Toggle visibility of text box based on status of check box -jQuery
问题描述
我有一些帮助,这一点早,但不知何故,我再也无法得到它的工作。我需要它未选中文本框时,如果一个其他复选框出现,并dissappear。任何人都知道怎么办?
<!DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0 Strict标准// EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< HTML的xmlns =http://www.w3.org/1999/xhtml郎=ENXML:LANG =EN>
< HEAD>
<冠军>更圆角(单幅图像的方法)使用CSS - 简单的例子< /标题>
<链接相对=样式表的href =演示/ dialog.css媒体=屏幕/>
<风格类型=文本/ CSS>
/ *基本的格式* /
身体{字体:76%的普通宋体,宋体,Arial字体,无衬线;}
H1 {字体:2.5em格鲁吉亚,宋体,黑体,宋体,无衬线;}
p {line-height的:1.6em;}
< /风格>
<脚本类型=文/ JavaScript的SRC =jquery.js和>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){$('#等)。改变(函数(){
。$('#otherrace)父()切换为(this.checked);
});
< / SCRIPT>
< /头>
<身体GT;
< DIV ID =演示>
<! - 最基本的例子 - >
< DIV CLASS =对话框中的>
< DIV CLASS =内容>
< DIV CLASS =T>< / DIV>
<! - 你的内容放在这里 - >
< BR />< BR />< BR />< BR />< BR />
<表格ID =Form1上的方法=邮报行动=><表格的宽度=100%的边界=0CELLSPACING =1的cellpadding =0>
&其中; TR>
&所述; TD宽度=16%>首先姓名和其中; / TD>
< TD宽度=32%><输入类型=文本名称=FNAME大小=25最大长度=25/>< / TD>
< TD宽度=9%>街< / TD>
< TD宽度=24%><输入类型=文本名称=街道大小=40/>< / TD>
< / TR>
&其中; TR>
< TD> MI< / TD>
< TD><输入类型=文本名称=米大小=3最大长度=1/>< / TD>
< TD>城市和LT; / TD>
< TD><输入类型=文本名称=城市大小=30/>< / TD>
< / TR>
&其中; TR>
< TD>姓< / TD>
< TD><输入类型=文本名称=FNAME大小=25最大长度=25/>< / TD>
< TD>县及LT; / TD>
< TD>&安培; NBSP;< / TD>
< / TR>
&其中; TR>
< TD> DOB< / TD>
< TD><输入类型=文本名称=年龄大小=5最大长度=3/>< / TD>
< TD>邮政编码:LT; / TD>
< TD><输入名称=拉链式=文字大小=5最大长度=7/>< / TD>
< / TR>
&其中; TR>
< TD>< P>性别< / P>
&其中p为H.;&安培; NBSP;&所述; / P>&所述; / TD>
< TD>
&其中p为H.;&所述; / P>
&其中p为H.;
<标签>
<输入类型=无线电名称=性别值=女ID =gender_0/>
< /标签>
女< BR />
<标签>
<输入类型=无线电名称=性别值=男ID =gender_1/>
男< /标签>
< BR />
&所述; / P>
&其中p为H.;&所述; / P>
< / TD>
< TD>&安培; NBSP;< / TD>
< TD>&安培; NBSP;< / TD>
< / TR>
< /表>
< P>什么比赛你认为自己?请选择所有适用< / P><输入类型=复选框名称=种族值=亚洲/>亚洲< BR /><输入类型=复选框名称=比赛值=夏威夷/>夏威夷原住民或其他太平洋岛民< BR /><输入类型=复选框名称=种族值=noanswer/>选择不回答< BR /> <输入类型=复选框名称=种族值=其他ID =其他/>其他指定< BR />< DIV ID =其他><输入类型=显示:无;类型=文字大小=25最大长度=25ID =otherrace/>< / DIV>< BR />
< DIV ID =种族>< / DIV>
< BR />< BR />
< BR />< BR />< BR />< BR />
< p ALIGN =权利与GT;<输入类型=按钮ALIGN =正确的价值=下一页的onclick =window.location.href ='registry2.html'/>< / P>
< /形式GT;
< BR />
< BR />< BR />< BR />
< / DIV>
< DIV CLASS =B>< DIV>< / DIV>< / DIV>
< / DIV>
< /身体GT;
< / HTML>
试试这个:
$(文件)。就绪(函数(){
$('#等)。改变(函数(){
$('#otherrace)切换为(this.checked);
});
});
请参阅它的工作: http://jsfiddle.net/karim79/2hw89/
此外,你有两个元素的id =其他的文件,这是的无效的。
在最后,你可以让你的表单更可用通过集中复选框被选中时,其他的文字输入,不必在家中就可以节省用户,这样一点点:
$('#等)。改变(函数(){
$('#otherrace)切换(this.checked).focus()。
});
演示: http://jsfiddle.net/karim79/2hw89/1/
I had some help with this earlier and yet somehow I can no longer get it to work. I need a text box to appear if an 'other' check box is checked, and dissappear when it isn't checked. Anyone know what gives?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Even More Rounded Corners (Single Image Approach) Using CSS - Simple Example</title>
<link rel="stylesheet" href="demo/dialog.css" media="screen" />
<style type="text/css">
/* basic formatting */
body {font:76% normal verdana,tahoma,arial,"sans serif";}
h1 {font:2.5em georgia,"times new roman",helvetica,verdana,"sans serif";}
p {line-height:1.6em;}
</style>
<script type="text/javascript" src="jquery.js">
<script type="text/javascript">
$(document).ready(function() { $('#other').change(function(){
$('#otherrace').parent().toggle( this.checked );
});
</script>
</head>
<body>
<div id="demo">
<!-- most basic example -->
<div class="dialog">
<div class="content">
<div class="t"></div>
<!-- Your content goes here -->
<br/><br/><br/><br/><br/>
<form id="form1" method="post" action=""><table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="16%">First Name</td>
<td width="32%"><input type="text" name="fname" size="25" maxlength="25" /></td>
<td width="9%">Street</td>
<td width="24%"><input type="text" name="street" size="40" /></td>
</tr>
<tr>
<td>MI</td>
<td><input type="text" name="mi" size="3" maxlength="1" /></td>
<td>City</td>
<td><input type="text" name="city" size="30" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="fname" size="25" maxlength="25" /></td>
<td>County</td>
<td> </td>
</tr>
<tr>
<td>DOB</td>
<td><input type="text" name="age" size="5" maxlength="3" /></td>
<td>ZIP</td>
<td><input name="zip" type="text" size="5" maxlength="7" /></td>
</tr>
<tr>
<td><p>Gender</p>
<p> </p></td>
<td>
<p></p>
<p>
<label>
<input type="radio" name="gender" value="female" id="gender_0" />
</label>
Female<br />
<label>
<input type="radio" name="gender" value="male" id="gender_1" />
Male</label>
<br />
</p>
<p></p>
</td>
<td> </td>
<td> </td>
</tr>
</table>
<p>What race do you consider yourself? Please choose all that apply.</p><input type="checkbox" name="race" value="asian" />Asian<br /><input type="checkbox" name="race" value="hawaii" />Native Hawaiian or other Pacific Islander<br /><input type="checkbox" name="race" value="noanswer" />Choose not to answer<br /><input type="checkbox" name="race" value="other" id="other" />Other, specify<br /><div id="other"><input style="display:none;" type="text" size="25" maxlength="25" id="otherrace" /></div><br />
<div id="race"></div>
<br/><br/>
<br/><br/><br/><br/>
<p align="right"><input type="button" align="right" value="Next page" onclick="window.location.href='registry2.html'" /></p>
</form>
<br/>
<br/><br/><br/>
</div>
<div class="b"><div></div></div>
</div>
</body>
</html>
Try this:
$(document).ready(function() {
$('#other').change(function(){
$('#otherrace').toggle(this.checked);
});
});
See it working: http://jsfiddle.net/karim79/2hw89/
Also, you have two elements with id="other" in your document, which is invalid.
Lastly, you can make your form a tiny bit more usable by focusing on the 'other' text input when the checkbox is checked, saving the user from having to home in on it, like this:
$('#other').change(function(){
$('#otherrace').toggle(this.checked).focus();
});
Demo: http://jsfiddle.net/karim79/2hw89/1/
这篇关于文本框的基础上的复选框状态切换的知名度-jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!