简体   繁体   English

是否选中“获取”复选框

[英]Get checkbox is checked or not using Javascript

I've got this problem that I can't solve. 我遇到了我无法解决的问题。 Partly because I can't explain it with the right terms. 部分原因是我无法用正确的术语进行解释。 I'm new to this so sorry for this clumsy question. 我对此很陌生,所以很抱歉这个笨拙的问题。

Below you can see an overview of my goal. 您可以在下面看到我的目标概述。

I'm running this code in Magento 我在Magento中运行此代码

<input type="checkbox" style="margin-left:24px" class="mydelete" name="checkAll" onclick='checkedAll(testCheck);' />

Added an id attribute to the element. 向该元素添加了id属性。

<input type="checkbox" style="margin-left:24px" class="mydelete" name="checkAll" id="checkAll"onclick='checkedAll();' />

Javascript Java脚本

function checkedAll(){
    if (document.getElementById('checkAll').checked) {
        alert("checked");
    } else {
        alert("You didn't check it! Let me check it for you.")
    }
}

DEMO 演示

If you are flexible with using jQuery , you can easily do something like - 如果您灵活使用jQuery ,则可以轻松执行以下操作-

$("input:checkbox").is(":checked")

which will return a boolean value based on whether that input box is checked or not. 它将根据是否选中该输入框返回布尔值。

You can do this using below code. 您可以使用以下代码执行此操作。

var checkprop = document.getElementsByName("checkAll").checked;
alert(checkprop);

EDIT 编辑

function checkedAll()
{
    var theform = document.theform;
    if(theform.checkAll.checked )
    {
        alert("checkAll Checked");
    } else {
        alert("Nothing Selected");
    }
}
<form name="theform">
    <input type="checkbox" style="margin-left:24px" class="mydelete" name="checkAll" onclick='checkedAll();' />
</form>

If you want to check if the checkbox is checked: 如果要检查复选框是否已选中:

<script>
function displayResult()
{
var x=document.getElementById("checkboxid");
alert(x.checked);
}
</script>

and in your body: 在你体内

<button type="button" onclick="displayResult()">Display value</button>

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

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