我尝试了下面的代码,
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.jstree.js"></script>
</head>
<body>
<div id="jstree_id" class="demo">
<ul>
<li id="asia">
<a href="#" onClick="bridge('url','my_id')">asia</a>
<ul>
<li id="china">
<a href="#" onClick="bridge('url','my_id')">india</a>
</li>
<li id="japan">
<a href="#" onClick="bridge('url','my_id')">japan</a>
</li>
</ul>
</li>
<li id="usa">
<a href="#" onClick="bridge('url','my_id')">usa</a>
</li>
</ul>
</div>
<input type="text" id="com" onKeyUp="bridge('com_url','my_id')" />
<input type="text" id="person" onKeyUp="bridge('prsn_url','my_id')" />
<input type="button" value="click" onClick="bridge('url','my_id');" />
<script type="text/javascript" class="source">
$(function () {
$("#jstree_id").jstree({
"plugins" : [ "themes", "html_data", "checkbox", "sort", "ui" ]
});
});
function bridge(path, tag) {
// path & tag are required one for Ajax functions*
var checked_ids = [];
$("#jstree_id").jstree("get_checked",null,true).each(function () {
checked_ids.push(this.id);
});
var company = $("#com").val() || [];
var person = $("#person").val() || [];
console.log(company+person+checked_ids);
}
</script>
</body>
</html>
问题是:
当我点击checkbox
时,它只返回ids
已经检查过的Iv'e。如何获取当前已检查ids
的{{1}}?
我的代码的全部目的是针对checkboxes
和checkbox tree
到text
的所有组合搜索数据库。
答案 0 :(得分:3)
最好使用jstree支持的本机事件,而不是使用js事件:
<body>
<div id="jstree_id" class="demo">
<ul>
<li id="asia">
<a href="#">asia</a>
<ul>
<li id="china">
<a href="#">india</a>
</li>
<li id="japan">
<a href="#">japan</a>
</li>
</ul>
</li>
<li id="usa">
<a href="#">usa</a>
</li>
</ul>
</div>
<input type="text" id="com" onKeyUp="bridge('com_url','my_id')" />
<input type="text" id="person" onKeyUp="bridge('prsn_url','my_id')" />
<input type="button" value="click" onClick="bridge('url','my_id');" />
<script type="text/javascript" class="source">
$(function () {
$("#jstree_id").jstree({
"plugins" : [ "themes", "html_data", "checkbox", "sort", "ui" ]
});
});
$('#jstree_id').bind('change_state.jstree',function(){
bridge('url','my_id');
});
function bridge(path, tag) {
//path & tag are required one for Ajax functions
var checked_ids = [];
$("#jstree_id").jstree("get_checked",null,true).each(function () {
checked_ids.push(this.id);
});
var company = $("#com").val() || [];
var person = $("#person").val() || [];
console.log(company+person+checked_ids);
}
</script>
</body>
</html>
直接使用click事件不起作用,因为更改复选框状态的jstree事件会在click事件触发后触发
答案 1 :(得分:1)
function getCheckedIDs()
{
var elements = document.getElementsByTagName("INPUT");
var checkedArray = new Array();
for(var i=0;i<elements.length;i++)
{
if(elements[i].type === 'checkbox' && elements[i].checked)
{
checkedArray.push(elements[i].id);
}
}
return checkedArray;
}
您可以在更改复选框时调用此函数。
答案 2 :(得分:0)
$(document).ready(function() {
var entity=[];
$("input[type='checkbox']").click(function(){
entity = $('input:checkbox:checked[name=checked]').map(function () {
return $(this).attr("id");
}).get();
alert(entity);
});
});