链接下拉列表选择列表代码 -- 连接数据
Chained Drop Down Select List Code -- Connection Data?
我正在努力将我在网上找到的链式下拉功能改编到我的网站上。我在网上找到的一些代码如下。一个问题是我无法理解读取var connection = selected.data('connection');
连接属性是 jquery 共有的行吗?还是在代码中的某个位置设置?如果是这样,这组在代码中的什么位置?
$(function(){
var questions = $('#questions');
function refreshSelects(){
var selects = questions.find('select');
// Improve the selects with the Chose plugin
selects.chosen();
// Listen for changes
selects.unbind('change').bind('change',function(){
// The selected option
var selected = $(this).find('option').eq(this.selectedIndex);
// Look up the data-connection attribute
var connection = selected.data('connection');
// Removing the li containers that follow (if any)
selected.closest('#questions li').nextAll().remove();
if(connection){
fetchSelect(connection);
}
});
}
var working = false;
function fetchSelect(val){
if(working){
return false;
}
working = true;
$.getJSON('ajax.php',{key:val},function(r){
var connection, options = '';
$.each(r.items,function(k,v){
connection = '';
if(v){
connection = 'data-connection="'+v+'"';
}
options+= '<option value="'+k+'" '+connection+'>'+k+'</option>';
});
if(r.defaultText){
// The chose plugin requires that we add an empty option
// element if we want to display a "Please choose" text
options = '<option></option>'+options;
}
// Building the markup for the select section
$('<li>'
<p>'+r.title+'</p>'
<select data-placeholder="'+r.defaultText+'">'
'+ options +''
</select>'
<span class="divider"></span>'
</li>').appendTo(questions);
refreshSelects();
working = false;
});
}
$('#preloader').ajaxStart(function(){
$(this).show();
}).ajaxStop(function(){
$(this).hide();
});
// Initially load the product select
fetchSelect('productSelect');
});
看看 jQuery.data() 函数。此函数允许您存储和检索与指定元素关联的任意数据。
在您的示例中,"选项"具有名称连接的某些属性。查看代码,似乎属性连接用于决定在链中选择下一个链接中的哪个。
相关文章:
- websocket客户端在发送大数据时断开连接
- 是否自动更新套接字连接数据?socket.io
- 连接数据并使用D3在地图中弹出
- 如何在打开连接时从Javascript发送一些附加数据,如用户名或时间
- Node.js:在用户连接之前向客户端发送数据
- 使用节点JS连接和断开数据卡
- 如何使用peerJS对等连接接收数据
- imacros提取两部分数据+连接常量保存到文件javascript
- 当连接在一起时,如何使用两个部分构建 JSON 对象,生成正确的 JSON 数据路径
- 链接下拉列表选择列表代码 -- 连接数据
- 简单的 D3.js饼图转换*没有*数据连接
- 如何在 BreezeJS 元数据中定义矩阵/链接/交叉表连接
- 服务器发送的事件连接关闭或未接收到数据
- Socket.io 数据包在断开连接和重新连接之间丢失
- 来自两个控制器的角度连接数据
- MongoDB集合设计连接数据
- 在图表框架中连接数据点的算法
- 在Javascript函数中连接数据
- 如何连接数据url
- 绘制一条连接数据点的线