简体   繁体   English

将鼠标悬停在提交/锚点上时如何检查复选框是否已选中并显示警报提示用户这样做

[英]How to check if checkbox has been checked when hovering over an submit / anchor and display an alert prompting users to do so

I am a little lost in working on this and need some help please. 我对此事有点迷茫,请寻求帮助。 I have a checkbox for terms and conditions that needs to be pressed before users can be registered. 我有一个复选框,其中包含需要注册用户才能按下的条款和条件。 I have written some pseudo code in how I think it should be programmed. 我已经写了一些伪代码,说明如何编程。

//If user hovers over anchor
//Check to see if check box is checked
//If not checked
//Display alert saying "You need to check the terms and conditions"
//Else if - do not allow user to use anchor
//Else if user does check the box allow anchors to be used submitted

Hope that make sense. 希望有道理。 Here is some of the code I have written but I am getting nowhere! 这是我编写的一些代码,但我无济于事! Please help :) 请帮忙 :)

    $("#checkbox1").click(function() {
          $("#id-reg-submit").attr("disabled", !this.checked);
        });

This above code is what I am using at the moment for users to actually click the box and prevents the moving forward in the sign up process. 上面的代码是我目前用于用户实际单击该框并防止其在注册过程中前进的代码。

What I need to do is get this to interacted with the other links (social media signup) and the submit button, thus this is what I have so far: 我需要做的是使它与其他链接(社交媒体注册)和“提交”按钮进行交互,因此,到目前为止,这就是我要做的:

 $(".wp-social-login-provider").hover(function(){
                            alert("Click The Terms & Conditions!");
                        });

                        if($("#checkbox1").click(function() {
                          $("#id-reg-submit").attr("disabled", !this.checked);
                        });){
                            $(".wp-social-login-provider").hover(function(){
                            alert("Click The Terms & Conditions!");
                        });
                        }else{
                            alert("hi");
                        }

I have been playing with code hover on and off stated trying to figure this out and have wrapped some code in an if else statement. 我一直在尝试将代码悬停打开和关闭,试图弄清楚这一点,并将一些代码包装在if else语句中。

    if({$("#checkbox1").click(function() {
          $("#id-reg-submit").attr("disabled", !this.checked);
        }); &&  $( "a.wp-social-login-provider" ).hover(
          function() {
            $( this ).append( $( "<span> ***</span>" ) );
          }, function() {
            $( this ).find( "span:last" ).remove();
          }
        ); )} else{
            alert.("huhuuhu");
    }

As you can tell I need a little help! 如您所知,我需要一点帮助! Can anyone help me please? 谁能帮我吗?

Cheers, 干杯,

Max. 最高

I think this is what you want to achieve? 我认为这是您想要实现的目标? you can use event.preventDefault(); 您可以使用event.preventDefault(); to stop the form from submitting. 停止提交表单。

$('input[type=button]').on('mouseenter', function() {
if($('input[type=checkbox]').prop('checked')) { alert('true');} else {alert('false');}
});

http://jsfiddle.net/c1d7j70u/ http://jsfiddle.net/c1d7j70u/

Here is an efficient solution as per Jquery standard.

JSFIDDLE JSFIDDLE

<input type="checkbox" id="chkBox"/>
<input type="button" id="btn" value="Hello World">
    <a href="#" id="linkTo">MY link</a>






 $("#btn, #linkTo").on("mouseenter",function(){
   // $("#chkBox").is(":checked") ? alert("checked"): alert("not checked");   
});

$("#btn, #linkTo").on("click",function(e){
    $("#chkBox").is(":checked") ? alert("call your function and replace the alert"): e.preventDefault();
});

There are a few possible ways of getting this done, but since you have already provided a fiddle, I will follow your logic from there :) 有几种方法可以完成此操作,但是由于您已经提供了小提琴,因此我将按照您的逻辑:)

Your logic in the fiddle you have provided is actually sound (sans a few typos), and I have expanded on it based on your requirements. 您提供的小提琴中的逻辑实际上是合理的(没有错别字),我已根据您的要求对此进行了扩展。 Basically what you want to do is to disable the anchor link when it is hovered on, but only when the terms and conditions are not accepted/checked. 基本上,您要做的就是在锚链接悬停时禁用锚链接,但仅在不接受/检查条款和条件时才禁用。 Therefore, you can use a namespaced event (in case if you have other click events bound to it), which prevents the default link action from executing using e.preventDefault() . 因此,您可以使用命名空间事件(如果您绑定了其他单击事件),这可以防止使用e.preventDefault()执行默认链接操作。 You turn this off if the if condition evaluates otherwise: 如果if条件的计算结果相反,则将其关闭:

$('input[type=button], .something').on('mouseenter', function () {
    if(!$('input[type="checkbox"]').prop('checked')) {
        alert('Please accept the terms and conditions');
        $('.something').on('click.disable', function(e) {
            e.preventDefault();
        });
    } else {
        $('.something').off('click.disable');
    }
});

See fiddle here: http://jsfiddle.net/teddyrised/znrrsxyr/1/ 在此处查看小提琴: http : //jsfiddle.net/teddyrised/znrrsxyr/1/

The only flaw of this system is that if a user tabs his way to the anchor link, he/she can actually use the link because the if conditionals are only evaluated on mouseenter. 该系统的唯一缺陷是,如果用户使用Tab键导航到锚链接,则他/她实际上可以使用该链接,因为if条件仅在mouseenter上评估。


An alternative approach is instead of listening to events on the targets (button and anchor), you can listen to the onChange events of your checkbox, and decide if you want to enable the targets accordingly: 一种替代方法是,您可以侦听复选框的onChange事件,并决定是否要相应地启用目标,而不是侦听目标(按钮和锚点)上的事件:

// Function: disable interactions
var disableActions = function() {
    $('.something').on('click.disable', function(e) {
        e.preventDefault();
    });
    $('input[type="button"]').prop('disabled');
    $('input[type="button"], .something').on('mouseenter.disable', function() {
        alert("Please check the terms and conditions.");
    });
}

// Disable interactions at runtime
disableActions();

// Conditional onchange
$('input[type="checkbox"]').change(function() {
    if(!$(this).prop('checked')) {
        disableActions();
    } else {
        $('input[type="button"]').prop('disabled', false);
        $('.something').off('click.disable');
        $('input[type="button"], .something').off('mouseenter.disable');
    }
});

See alternative solution here: http://jsfiddle.net/teddyrised/znrrsxyr/4/ 在此处查看替代解决方案: http : //jsfiddle.net/teddyrised/znrrsxyr/4/

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

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