[英]Javascript checkbox toggle issue
Hi I'm trying to get a simple script that'll hide or show a div based on if a user clicks a checkbox on the page. 嗨,我正在尝试获取一个简单的脚本,该脚本将根据用户是否单击页面上的复选框来隐藏或显示div。
I have a test HTML written with the elements I want to edit, along with the script but when I test it firebug now chrome developer consoles shows an error or the script even being called. 我有一个包含要编辑的元素以及脚本的测试HTML,但是当我对其进行测试时,firebug现在chrome开发者控制台显示错误或脚本甚至被调用。
I've looking at both of these for reference: 我已经看了这两个作为参考:
How to check whether a checkbox is checked in jQuery? 如何检查jQuery中是否已选中复选框?
How to grey out checkbox unless radio button is selected? 除非选中单选按钮,否则如何使复选框变灰?
This is the reference to the jQuery library I'll be using. 这是我将要使用的jQuery库的参考。
<script language="JavaScript" type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
As well as the script I'm trying to get working: 以及我要开始工作的脚本:
$('extendedStay').click(function(){
$('#extendedQuarter').toggle(this.checked);
});
And the elements I'm trying to get working: 我正在努力工作的要素:
<div id="extendedResponse" align="left">
<form action="">
<div name="extendedStayResponses">
<input type="checkbox" name="extendedStay" id="extendedStay"/>Yes
</div>
<div id="extendedQuarter" style="display:none">
<select name="extraQuarters" selected="1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
</form>
</div>
Am I just improperly implementing the function or is there some syntax error I', not seeing. 我是不是正确地实现了该功能,还是我没有看到语法错误?
Add a #
for id selectors. 为ID选择器添加#
。 Also if this script is executing on page load you should place it without a $(document).ready()
function. 另外,如果此脚本在页面加载时执行,则应将其放置为不带有$(document).ready()
函数。
<script>
$(document).ready(function(){
$('#extendedStay').click(function(){
$('#extendedQuarter').toggle(this.checked);
});
});
</script>
Working Example: http://jsfiddle.net/Lv8eX/ 工作示例: http : //jsfiddle.net/Lv8eX/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.