简体   繁体   English

如何验证复选框是否被选中?

[英]How to verify if a check box is checked?

I am currently trying to make my checkbox show a popup for browser notifications if it is checked.我目前正在尝试让我的复选框在选中时显示浏览器通知的弹出窗口。 If the user unchecked it the browser notification will popup to declined vice-versa.如果用户取消选中它,浏览器通知将弹出拒绝,反之亦然。

here is a snippet of what I am trying right now but it is not working.这是我现在正在尝试的一个片段,但它不起作用。

if(checkbox_id == (queue_notification)) {
        if(checkbox_state) {
          $('input#user_hop_queue_notification').is(':checked') == '1'
             if(Notification.requestPermissionre !== "granted"){
              Notification.requestPermission(function(status) {
                console.log('Notification permission status:', status);
              });
            }
          }
        else {
          $('input#user_hop_queue_notification').is(':checked')== ('0');
          if(Notification.requestPermission !== "denied"){
              Notification.requestPermission(function(status) {
                console.log('Notification permission status:', status);
              });
            }
          }
      }

The Notification Api is something that the user can deny also so you even if you set the Notification.permission property to granted that will not work. Notification granted是用户也可以拒绝的内容,因此即使您将Notification.permission属性设置为 grant 也不起作用。 So your best chance is to use the Notification.requestpermission method and check if the user has granted notification access then use to notification api to show notifications.因此,您最好的机会是使用Notification.requestpermission方法并检查用户是否已授予通知访问权限,然后使用通知 api 来显示通知。 Thanks.谢谢。

Your code looks very weird, you seem to test the value of is(':checked') , but you actually do not (no if -statement??).您的代码看起来很奇怪,您似乎测试了is(':checked')的值,但实际上没有(没有if语句??)。 There is a variable checkbox_state which is tested but not set?有一个变量checkbox_state已测试但未设置? And, you seem to test if the value of is(':checked') returns the string 0 or 1 .而且,您似乎在测试is(':checked')的值是否返回字符串01 While this works, if you test this manually in the console, or check the documentation, you would see it returns true or false , so that could make your code a lot simpler too.虽然这可行,但如果您在控制台中手动测试,或查看文档,您会看到它返回truefalse ,这样也可以使您的代码更简单。

So I would write your code as follows:所以我会写你的代码如下:

  if(checkbox_id == (queue_notification)) {
    checkbox_state = $('input#user_hop_queue_notification').is(':checked'); 
    if(checkbox_state) {
      if(Notification.requestPermissionre !== "granted"){
          Notification.requestPermission(function(status) {
            console.log('Notification permission status:', status);
          });
      } 
    } else {
      if(Notification.requestPermission !== "denied"){
          Notification.requestPermission(function(status) {
            console.log('Notification permission status:', status);
          });
      }
    }
  }

If I understood your intention correctly, this should work more as expected.如果我正确理解了您的意图,这应该会按预期工作。 We could refactor the check_state variable away, as we have no further use for it (but for now I wanted to stay closer to your original code)(it might still improve readability, so that would be a good reason to keep it, however the is(:checked) is pretty self-explanatory on its own).我们可以重构check_state变量,因为我们没有进一步使用它(但现在我想更接近您的原始代码)(它可能仍会提高可读性,所以这将是保留它的一个很好的理由,但是is(:checked)本身是不言自明的)。

If you are trying to set the checked state, you should use the following code:如果您尝试设置选中的 state,则应使用以下代码:

 $('input#user_hop_queue_notification').prop('checked', true);

(if not obvious, true will "check" the checkbox, false will uncheck). (如果不明显,true 将“选中”复选框,false 将取消选中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM