use*_*094 2 checkbox jquery show-hide
我可能盯着它看了太长时间,并且缺少明显的东西,但是:为什么在JSFiddle中可以正常工作,而在上载时却不能正常工作?
最后的警报显示在测试网站上,但是无论如何也可以看到“ showthis”。
的HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Form Field</title>
<script type="text/javascript" src="scripts/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="scripts/showfield.js"></script>
</head>
<body>
<input type="checkbox" name="checkbox" id="checkbox" value="checkbox" />
<br />
<input id="showthis" name="showthis" size="50" type="text" value="text here"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
jQuery(我下载了一个缩小的jQuery 1.11.0,这在JSFiddle中是有效的)
//hide field by default
$('input[name="showthis"]').hide();
//show it when the checkbox is clicked
$('input[name="checkbox"]').on('click', function(){
if ( $(this).prop('checked') ) {
$('input[name="showthis"]').fadeIn();
}
else {
$('input[name="showthis"]').hide();
}
});
// the alert is showing fine
alert ("hello");
Run Code Online (Sandbox Code Playgroud)
JSFiddle http://jsfiddle.net/DLQY9/
将代码包装在加载事件中。这是较新的首选语法(而不是$(window)or $(document)):
$(function () {
$('input[name="showthis"]').hide();
//show it when the checkbox is clicked
$('input[name="checkbox"]').on('click', function () {
if ($(this).prop('checked')) {
$('input[name="showthis"]').fadeIn();
} else {
$('input[name="showthis"]').hide();
}
});
});
Run Code Online (Sandbox Code Playgroud)