jQuery - 基于复选框禁用 href - 脚本的位置
jQuery - Disable href based on checkbox - Location of script?
不知道我做错了什么,但我是一个JavaScript和html的新手,所以可能很多。 我正在尝试根据复选框的选择启用/禁用 href 链接。 这是代码:
<!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>Page Title</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;"/>
<meta http-equiv="keywords" content="keyword1, keyword2" />
<meta http-equiv="description" content="Description of Page" />
<link href="css/style.css"rel="stylesheet" type="text/css" />
<style type="text/css">
.auto-style1 {
font-size: 12px;
color: #4d4d4d;
line-height: 2em;
margin: 5px;
padding: 0;
text-align: center;
}
.auto-style2 {
text-align: center;
}
.auto-style3 {
font-family: Neogrey;
}
</style>
<script type="jQuery">
$('#link').click(function(){return false; });
$('#check').click(function() {
if(!$(this).is(':checked')){
$('#link').bind('click', function(){ return false; });
}else{
$('#link').unbind('click');
}
});</script>
</head>
<body>
<div class="auto-style2">
<form id="form1" runat="server">
<div class="logo"><h1 class="auto-style3">
</h1></div>
<h2 class="head">Header Text</h2>
<div class="auto-style1">
Welcome Text.<br />
<input type="checkbox" id="check"/><label for="check">I accept the <a href="TC.html">Terms of Service</a>.</label>
<br />
<a href="http://www.google.com" id="link">
<img alt="" height="30" src="images/download.png" width="100" /></a><br />
</div>
<div class="footer">
<div class="menu">
<a href="iOS-TC.html">iOS</a>
<a href="android-TC.html">Android</a>
<a href="blackberry-TC.html">Blackberry</a>
<a href="http://www.yahoo.com">Support</a>
</div>
</div>
</form>
</div>
</body>
</html>
任何帮助将不胜感激!
一旦你包含了jQuery,我建议使用DOM来使脚本更有效率:
// bind the click-handler:
$('#link').click(function(e){
// prevent the default action of clicking on a link:
e.preventDefault();
// if the element with id 'check' is checked:
if (document.getElementById('check').checked){
// change the window.location to the 'href' of the clicked-link:
window.location = this.href;
}
});
JS小提琴演示。
这将导致<head>
元素的以下 HTML:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Title</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;"/>
<meta http-equiv="keywords" content="keyword1, keyword2" />
<meta http-equiv="description" content="Description of Page" />
<link href="css/style.css"rel="stylesheet" type="text/css" />
<style type="text/css">
/* CSS removed for brevity */
</style>
<!-- note that this must come before the script containing the jQuery code
which in this case follows immediately -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="jQuery">
$('#link').click(function(e){
e.preventDefault();
if (document.getElementById('check').checked){
window.location = this.href;
}
});
</script>
</head>
或者(尽管您仍然需要包含jQuery),您可以使用以下方法:
// binds a change-handler function to the check-box:
$('#check').change(function () {
/* adds the 'disabled' class to the '#link' element if
the '#check' element is _not_ checked, and removes the
class if the '#check' element _is_ checked: */
$('#link').toggleClass('disabled', !this.checked);
/* triggers the change event-handler (so the class-name is toggled
appropriately on page-load: */
}).change();
/* attaches a delegated click-handler to the 'body' element,
if the click occurs on an 'a' element with the 'disabled'
class the function is executed:
*/
$('body').on('click', 'a.disabled', function(e){
// the default behaviour is prevented
e.preventDefault();
});
JS小提琴演示。
引用:
-
change()
. -
click()
. -
document.getElementById()
. -
event.preventDefault()
. -
on()
. -
toggleClass()
. -
window.location
.
在 和 标签之间放置以下行以便能够使用 jquery:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
更改您的行,内容如下:
<script type="jQuery">
自:
<script type="text/javascript">
jsFiddle 示例
正如 Tom Gerken 是第一个指出的那样,你必须包括 jQuery 库,正如他在回答中演示的那样。这确实是解决问题的关键点,汤姆应该因正确的解决方案而获得荣誉。
但是,以下是启用/禁用链接的另一种方法:
首先将 href 设置为 "#"
,如下所示:
<a href="#" id="link">
你的jQuery代码块将执行以下操作:
$('#check').click(function() {
if( $(this).is(':checked') ){
$('#link').attr('href','http://www.google.com');
}else{
$('#link').attr('href','#');
}
});
完整代码示例:
<!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>Page Title</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;"/>
<meta http-equiv="keywords" content="keyword1, keyword2" />
<meta http-equiv="description" content="Description of Page" />
<link href="css/style.css"rel="stylesheet" type="text/css" />
<style type="text/css">
.auto-style1 {
font-size: 12px;
color: #4d4d4d;
line-height: 2em;
margin: 5px;
padding: 0;
text-align: center;
}
.auto-style2 {
text-align: center;
}
.auto-style3 {
font-family: Neogrey;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<style>
</style>
<script type="text/javascript">
$(document).ready(function() {
$('#link').click(function(){return false; });
$('#check').click(function() {
if( $(this).is(':checked') ){
$('#link').attr('href','http://www.google.com');
}else{
$('#link').attr('href','#');
}
});
}); //END $(document).ready()
</script>
</head>
<body>
<div class="auto-style2">
<form id="form1" runat="server">
<div class="logo"><h1 class="auto-style3">
</h1></div>
<h2 class="head">Header Text</h2>
<div class="auto-style1">
Welcome Text.<br />
<input type="checkbox" id="check"/><label for="check">I accept the <a href="TC.html">Terms of Service</a>.</label>
<br />
<a href="#" id="link">
<img alt="" height="30" src="images/download.png" width="100" /></a><br />
</div>
<div class="footer">
<div class="menu">
<a href="iOS-TC.html">iOS</a>
<a href="android-TC.html">Android</a>
<a href="blackberry-TC.html">Blackberry</a>
<a href="http://www.yahoo.com">Support</a>
</div>
</div>
</form>
</div>
</body>
</html>
相关文章:
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 借助asp.net验证或java脚本对多个文本进行验证
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- Java脚本时间添加
- 如何使用<脚本>更改 href
- jQuery - 基于复选框禁用 href - 脚本的位置
- 在 java 脚本中使用 href 更改图像
- 如何在 java 脚本中的 Location.href 中发送大型数据
- 将location.href添加到脚本src中
- 调用window.location.href后如何执行脚本
- 如何保护 Location.href 免受 JavaScript 中跨站点脚本的影响
- 如何获得值php的ajax脚本onclick函数使用<一个href [FINISH]
- Javascript:在location.href之后执行脚本
- 谷歌标签管理器脚本在头won'不允许在href中进行更改
- 使用location.href传递值给PHP脚本
- 用//替换href http://的脚本
- 不能在jquery移动上使用脚本href = #page-index更改页面
- 脚本里面的href how to
- dojo contentPane,我张贴html代码与href在我的contentPane.html代码有一个脚本,但不
- 停止脚本更改document.location.href