Php 链接下拉列表选择列表代码--连接数据?
我正在将我在网上找到的链接式下拉功能应用到我的网站上。下面是我在网上找到的一些代码。一个问题是我很难理解读取Php 链接下拉列表选择列表代码--连接数据?,php,javascript,jquery,html,ajax,Php,Javascript,Jquery,Html,Ajax,我正在将我在网上找到的链接式下拉功能应用到我的网站上。下面是我在网上找到的一些代码。一个问题是我很难理解读取var connection=selected.data('connection')的行连接属性是jquery的常见属性吗?或者是在代码中的某个地方设置的?如果是,该集合在代码中的何处 $(function(){ var questions = $('#questions'); function refreshSelects(){ var selects
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');
});
$(函数(){
变量问题=$(“#问题”);
函数refreshSelects(){
var selects=questions.find('select');
//使用Selected插件改进selects
selects.selected();
//倾听变化
选择.unbind('change')。bind('change',function(){
//所选选项
所选变量=$(this.find('option').eq(this.selectedIndex);
//查找数据连接属性
var connection=selected.data('connection');
//卸下下面的li容器(如果有)
选中。最近(“#问题li”).nextAll().remove();
如果(连接){
获取选择(连接);
}
});
}
var=false;
函数fetchSelect(val){
如果(工作){
返回false;
}
工作=真实;
$.getJSON('ajax.php',{key:val},函数(r){
变量连接,选项=“”;
$。每个(r.项目,功能(k,v){
连接=“”;
如果(v){
连接='数据连接='+v+'';
}
选项+=''+k+'';
});
if(r.defaultText){
//选择的插件要求我们添加一个空选项
//元素,如果要显示“请选择”文本
选项=“”+选项;
}
//为“选择”节生成标记
$('\
“+r.title+”\
\
“+选项+”\
\
\
附件(问题);
刷新选择();
工作=假;
});
}
$('#preload').ajaxStart(函数(){
$(this.show();
}).ajaxStop(函数(){
$(this.hide();
});
//最初加载产品选择
fetchSelect('productSelect');
});
看看jQuery.data()函数。此函数允许您存储和检索与指定元素关联的任意数据
在您的示例中,“option”具有名称连接的某些属性。查看代码,属性连接似乎用于决定在链中选择下一个链接中的哪一个 我理解这一点,但代码中没有data()方法用于将属性名为“connection”的信息分配给选项对象。data-*是HTML5数据属性。它们在代码中设置为数据连接,但可以作为数据获取()更多信息:天哪,我的朋友,阅读你的代码连接=“”;如果(v){connection='data connection=“'+v+'”;}options+=''+k+'';对不起,我弄错了。但是你的链接不是说我们必须使用attr()方法而不是data()方法来检索使用data-*语法存储的数据吗?这个链接是为了给你更多关于HTML5数据属性的信息。通过新的jquery更新,您可以使用data()