简体   繁体   English

jQuery复选框验证(不带表单)

[英]jQuery checkbox validation without form

I want to make a validation function with jQuery or pure javascript. 我想使用jQuery或纯JavaScript进行验证。

this is my checkbox 这是我的复选框

<input type="checkbox" name="terms" id="terms">

and this is my link button 这是我的链接按钮

<label id="kosullar" for="terms">
<a class="fancybox-effects-d" data-fancybox-type="iframe" onclick="" href="kosullar.php">KULLANIM  &nbsp;KOSULLARINI  &nbsp;KABUL&nbsp; EDIYORUM</a>
</label>

If checkbox is checked process will continue to redirect my href url if not i want to show alert() to user. 如果选中了复选框,则过程将继续重定向我的href网址(如果没有,我想向用户显示alert())。

Thats it. 而已。

Would you show me an example? 你能给我一个例子吗?

In your onclick define a function and pass this , return false to stop the default action 在您的onclick定义一个函数并传递this函数, return false停止默认操作

onclick="verifyCheck(this); return false;"

function verifyCheck(elem) {
    var cb = document.getElememtById("terms");

    if (cb.checked) { 
        location.href = elem.href;
    } else {
         alert("Check the box!");
    }
}
$('.fancybox-effects-d').click(function(e) {
    if (!($('#fancybox-effects-d').is(':checked'))) {
        e.preventDefault();
    }
});

Try this. 尝试这个。

 <a class="fancybox-effects-d" data-fancybox-type="iframe" onclick="return Validate();" href="kosullar.php">

Javascript method Javascript方法

function Validate()
{
 If($("#terms").is(":checked"))
 {
    //do your code to go to next step
 }
 else
 {
  alert("Please check the checkbox");
  return false;
 }
}

Can you try this, 你可以试试这个吗

     function Accept(dat){

            var terms = $('#terms').is(':checked');

            if(terms){
                window.location.href=dat.href;
            }else{
                alert('not checked!');
                return false;
            }
      }

HTML Section: HTML部分:

    <input type="checkbox" name="terms" id="terms">

    <label id="kosullar" for="terms">
        <a class="fancybox-effects-d" data-fancybox-type="iframe" onclick="return Accept(this);" href="kosullar.php">KULLANIM  &nbsp;KOSULLARINI  &nbsp;KABUL&nbsp; EDIYORUM</a>
    </label>

Another method: 另一种方法:

 $(function(){

      $(".fancybox-effects-d").click(function(){

           var terms = $('#terms').is(':checked');

            if(terms){
                window.location.href=$(this).attr('href');
            }else{
                alert('not checked!');
                return false;
            }
        });

   });

HTML: HTML:

<input type="checkbox" name="terms" id="terms">

    <label id="kosullar" for="terms">
        <a class="fancybox-effects-d" data-fancybox-type="iframe"  href="kosullar.php">KULLANIM  &nbsp;KOSULLARINI  &nbsp;KABUL&nbsp; EDIYORUM</a>
</label>

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

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