onclick复选框事件 [英] onclick checkbox event

查看:103
本文介绍了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"/> &#160;  
    Offices<input class="LocType" type="checkbox" value="Office"/> &#160;  
    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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆