简体   繁体   English

如何使用 Javascript 根据是否选中复选框显示警告框

[英]How to display alert boxes based on if a check box is checked or not using Javascript

I am trying to make it so that if the checkbox on our page is checked it will display an alert message notifying the user they have opted in to showing their checkout history.我试图做到这一点,如果我们页面上的复选框被选中,它将显示一条警告消息,通知用户他们已选择显示他们的结帐历史记录。 If the user deselects the check box then it should show another alert box letting them know they are opting out of showing their checkout history.如果用户取消选中该复选框,那么它应该显示另一个警告框,让他们知道他们选择不显示他们的结帐历史记录。 I am having trouble just displaying alert boxes if the check box is even checked/unchecked.如果复选框被选中/未选中,我在显示警告框时遇到问题。 Here is my code.这是我的代码。

HTML HTML

<div class="myAccountCheckboxHolder" id="showCheckoutHistoryCheckbox">
    <input tabindex="40" checked="checked" id="showCheckoutHistory" name="showCheckoutHistory" type="checkbox">
        <label for="showCheckoutHistory" class="checkLabel">Show my checkout history</label>
    </div>    

Javascript Javascript

function validate() {
    var checkoutHistory = document.getElementById('showCheckoutHistory');
    if (checkoutHistory.checked) {
        alert("You have elected to show your checkout history.");
    } else {
        alert("You have elected to turn off checkout history.");
    }

Thank you.谢谢你。

jQuery (original answer) jQuery(原始答案)

jQuery(document).ready(function() {
    jQuery('#showCheckoutHistory').change(function() {
        if ($(this).prop('checked')) {
            alert("You have elected to show your checkout history."); //checked
        }
        else {
            alert("You have elected to turn off checkout history."); //not checked
        }
    });
});

Documentation here: http://api.jquery.com/prop/ 文档: http//api.jquery.com/prop/


JavaScript (2018 update) JavaScript(2018更新)

It is worth to notice, that you don't need any javascript libraries to acomplish that. 值得注意的是,您不需要任何javascript库来实现它。

// Check current state and save it to "checked" variable
var checked = document.getElementById("showCheckoutHistory").checked; 

// Set state manually (change actual state)
document.getElementById("showCheckoutHistory").checked = true; // or
document.getElementById("showCheckoutHistory").checked = false;

For more pure javascript solutions I recommend vanilla.js : http://vanilla-js.com/ framework ;) 对于更纯粹的JavaScript解决方案,我推荐vanilla.jshttp//vanilla-js.com/ framework;)

So for checkbox changes I use the change listener that jQuery provides. 因此,对于复选框更改,我使用jQuery提供的change侦听器。 So make your javascript this: 所以让你的javascript:

$("#showCheckoutHistory").change(function(event){
    if (this.checked){
        alert("You have elected to show your checkout history.");
    } else {
        alert("You have elected to turn off checkout history.");
    }
});

Here it is as a working fiddle . 在这里它是一个工作小提琴

Here's a working version without jQuery. 这是一个没有jQuery的工作版本。 Actually your code worked pretty well once the function had a closing brace. 实际上,一旦函数有一个右括号,你的代码就能很好地工作。 I just added in an event listener for you and moved the checkoutHistory var outside the function. 我刚刚为你添加了一个事件监听器,并将checkoutHistory var移到了函数之外。

var checkoutHistory = document.getElementById('showCheckoutHistory');
checkoutHistory.onchange = function() {
    console.log(checkoutHistory);
    if (checkoutHistory.checked) {
        alert("You have elected to show your checkout history.");
    } else {
        alert("You have elected to turn off checkout history.");
    }
}

And here's the JSFiddle I used. 这是我用过的JSFiddle。 http://jsfiddle.net/aaFe5/ http://jsfiddle.net/aaFe5/

$(document).ready(function(){
  $("input:checkbox").change(function(){

    var dynamicId = this.id;

    let checkBoxtoggle = $("#"+this.id)[0].checked;

    if(checkBoxtoggle!==false){
      alert("done");
    }else{
      alert("not");
    }
  });
});

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

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