onclick复选框事件 [英] onclick checkbox event
本文介绍了onclick复选框事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要做的就是选中复选框的onclick,将其值附加到URL上并重定向..我该怎么做?
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>
推荐答案
这确实不是一个好的UI设计-用户不希望通过复选框进行导航-但这是您想要的代码:
That's really not a good UI design - users do not expect navigation to occur via checkboxes - but here's the code you want:
$('.LocType').click (function () {
if (this.checked) {
// Do stuff
window.location.href = "http://www.yahoo.com?paramNameHere=" + this.value;
}
});
您不需要在函数内部使用$(this)
,因为this
足以检查checked
状态并获取value
.
You don't need $(this)
inside the function because this
is enough to just check the checked
state and get the value
.
您没有说出结果URL的外观,所以我只是在值上附加了通用的paramNameHere
参数.
You didn't say what the resulting URL should look like, so I've just appended the value with a generic paramNameHere
parameter.
这篇关于onclick复选框事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文