更改/获取CheckBox的检查状态 [英] Change/Get check state of CheckBox

查看:104
本文介绍了更改/获取CheckBox的检查状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想用JavaScript改变CheckBox的值。不是说我不能使用jQuery来做这件事。我试过这样的东西,但它不起作用。



JavaScript函数

  function checkAddress()
{
if(checkAddress.checked == true)
{
alert(a);


code

$ b

HTML

 < input type =checkboxname =checkAddressonchange =checkAddress()/> 


解决方案

使用 onclick 反而会起作用。从理论上讲,它可能无法捕捉通过键盘进行的更改,但所有浏览器在通过键盘进行检查时确实会触发事件。



您还需要将复选框传入函数:

 函数checkAddress(复选框)
{
if(checkbox.checked)
{
alert(a);


code

$ b

HTML

 < input type =checkboxname =checkAddressonclick =checkAddress(this)/> 


I just want to get/change value of CheckBox with JavaScript. Not that I cannot use jQuery for this. I've tried something like this but it won't work.

JavaScript function

    function checkAddress()
    {
        if (checkAddress.checked == true)
        {
            alert("a");
        }
    }

HTML

<input type="checkbox" name="checkAddress" onchange="checkAddress()" />

解决方案

Using onclick instead will work. In theory it may not catch changes made via the keyboard but all browsers do seem to fire the event anyway when checking via keyboard.

You also need to pass the checkbox into the function:

function checkAddress(checkbox)
{
    if (checkbox.checked)
    {
        alert("a");
    }
}

HTML

<input type="checkbox" name="checkAddress" onclick="checkAddress(this)" />

这篇关于更改/获取CheckBox的检查状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆