onclick 复选框事件
我想做的就是单击复选框,将其值附加到 URL 并重定向。我该怎么做?
$(document).ready(function() {
var url = 'http://mysite.com/results.aspx';
$('.LocType').click (function ()
{
var thisCheck = $(this);
if (thischeck.is (':checked'))
{
// Do stuff
window.location.href = "http://www.yahoo.com";
}
});
});
<div class="MyOptions">
Hospitals<input class="LocType" type="checkbox" value="Hospital"/>  
Offices<input class="LocType" type="checkbox" value="Office"/>  
Facilities<input class="LocType" type="checkbox" value="Facility"/>
</div>
All I am trying to do is onclick of checkbox, append its value to the URL and redirect..How do I do this??
$(document).ready(function() {
var url = 'http://mysite.com/results.aspx';
$('.LocType').click (function ()
{
var thisCheck = $(this);
if (thischeck.is (':checked'))
{
// Do stuff
window.location.href = "http://www.yahoo.com";
}
});
});
<div class="MyOptions">
Hospitals<input class="LocType" type="checkbox" value="Hospital"/>
Offices<input class="LocType" type="checkbox" value="Office"/>
Facilities<input class="LocType" type="checkbox" value="Facility"/>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这确实不是一个好的 UI 设计 - 用户不希望通过复选框进行导航 - 但这是您想要的代码:
您不需要在函数内使用
$(this)
因为this< /code> 足以检查
checked
状态并获取值
。您没有说明生成的 URL 应该是什么样子,因此我只是在该值后面附加了通用
paramNameHere
参数。That's really not a good UI design - users do not expect navigation to occur via checkboxes - but here's the code you want:
You don't need
$(this)
inside the function becausethis
is enough to just check thechecked
state and get thevalue
.You didn't say what the resulting URL should look like, so I've just appended the value with a generic
paramNameHere
parameter.如果您将函数设置为接收参数,则 JavaScript 会给您一个事件,该事件具有触发该事件的元素,您可以使用下一个代码来存档此事件:
if you set up your function to recive a parameter javascript would give you an event that have the ellement that fire the event, you cuold do archive this whit the next code: