我试图从使用 jquery-bonsai 创建的树结构中获取选定(选中)的值(链接:http://simonwade.me/jquery-bonsai/)
我的代码:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel='stylesheet' type='text/css' href='http://bgagenomics.iicb.res.in/bga/arup/js/assets/svg-icons.css' />
<link rel='stylesheet' type='text/css' href='http://bgagenomics.iicb.res.in/bga/arup/js/jquery.bonsai.css' />
<script src='http://bgagenomics.iicb.res.in/bga/arup/js/jquery.bonsai.js'></script>
<script src='http://bgagenomics.iicb.res.in/bga/arup/js/jquery.qubit.js'> </script>
<script>
jQuery(function() {
$('#auto-checkboxes').bonsai({
expandAll: false,
checkboxes: true, // qbit plung helper
createInputs: 'checkbox' // enebles auto click data-checked
});
});
$(document).ready(function(){
$("#val").click(function()
{
alert("hello"); //need help here
});
});
</script>
</head>
<body>
<div>
<ol id='auto-checkboxes' data-name='foo'>
<li data-value='0'>All
<ol>
<li data-value='1'>One</li>
<li data-value='2' data-id='2'>
Two
<ol>
<li data-name='baz' data-value='3'>
Three
<ol>
<li data-name='baz' data-value='4'>Four</li>
</ol>
</li>
<li data-value='5'>Five</li>
</ol>
</li>
</ol>
</li>
</ol>
</div>
<br>
<input id ="val" type="image" value = "Result" />
<body>
</html>
上面的代码将创建类似的树结构
我想在点击结果时获取检查值。
任何解决上述问题的帮助或建议都会有很大帮助。
最佳答案
您可以通过循环查找选定复选框来获取所有选定复选框的值:
var yourArray = [];//global variable
$(document).ready(function(){
$("#val").click(function() {
yourArray = [];//unset previous values
$("input:checkbox[name=yourCheckboxName]:checked").each(function(){
yourArray.push($(this).val());
});
});
});
关于javascript - 如何使用jquery获取jquery-bonsai树的选定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39100672/