怎么获取所有checkbox的值

怎么获取所有checkbox的值,第1张

jquery的遍历方法可以获取复选框所欲的选中值

$("input:checkbox:checked")each(function(index,element)); // 为所有选中的复选框执行函数,函数体中可以取出每个复选框的值

$("input:checkbox:checked")map(function(index,domElement)); // 将所有选中的复选框通过函数返回值生成新的jQuery 对象

实例演示:点击按钮获取checkbox的选中值

创建Html元素

<div class="box">

<span>点击按钮获取checkbox的选中值:</span>

<div class="content">

<input type='checkbox' name='message' value='1'/>发送短信

<input type='checkbox' name='message' value='2'/>发送邮件

</div>

<input type="button" value="提交">

</div>

设置css样式

divbox{width:300px;padding:20px;margin:20px;border:4px dashed #ccc;}

divbox>span{color:#999;font-style:italic;}

divcontent{width:250px;margin:10px 0;padding:20px;border:2px solid #ff6666;}

编写jquery代码

$(function(){

$("input:button")click(function() {

text = $("input:checkbox[name='message']:checked")map(function(index,elem) {

return $(elem)val();

})get()join(',');

alert("选中的checkbox的值为:"+text);

});

});

高版本的jq建议使用prop来 *** 作布尔值类型的属性。 $('#my_radio')prop('checked',true); 这个实现选中应该是没问题的,你可以打印一下这个radio的属性。 动态 *** 作不行的话,你应该在动态生成之后再 *** 作,一开始没生成的时候你应该也获取不到吧。

如果被选中则可以提交,没有选中d出提示框,不能提交。

<script language="javascript">

function check()

{

if(documentform1checkboxchecked==false) //如果表单1的复选框没被选中。则执行以下语句

{

alert("请选中复选框");

return false;

}

}

</script>

选取的集合不知道怎么获取,但是单项是可以判断的

下面的代码自己看吧

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemLinq;

using SystemText;

using SystemWindowsForms;

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            for (int i = 0; i < 15; i++)

            {

                checkedListBox1ItemsAdd(iToString());

            }

        }

        private void button1_Click(object sender, EventArgs e)

        {

            for (int i = 0; i < checkedListBox1ItemsCount; i++)

            {

                if (checkedListBox1GetItemChecked(i))

                {

                    checkedListBox2ItemsAdd(checkedListBox1Items[i]);

                }

            }

        }

    }

}

上图:

希望对你有帮助

js中获取checkbox选中的值的方法:

<script>

function checkbox()

{

var str=documentgetElementsByName("box");

var objarray=strlength;

var chestr="";

for (i=0;i<objarray;i++)

{

if(str[i]checked == true)

{

chestr+=str[i]value+",";

}

}

if(chestr == "")

{

alert("请先选择一个爱好");

}

else

{

alert("先择的是:"+chestr);

}

}

</script>

选择爱好:

<input type="checkbox" name="box" id="box1" value="跳水" />跳水

<input type="checkbox" name="box" id="box2" value="跑步" />跑步

<input type="checkbox" name="box" id="box3" value="听音乐" />听音乐

<input type="button" name="button" id="button" onclick="checkbox()" value="提交" />

以上就是关于怎么获取所有checkbox的值全部的内容,包括:怎么获取所有checkbox的值、bootstrap-treeview 怎么获取所有checked的节点、如何用JS获取check的状态等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9269610.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存