我有一个JQuery函数,可以在向按钮添加或删除href属性时更改按钮的颜色。如果我像这样用模糊启动它,它就能完美地工作。
$("#name").blur(function() {
var standards_name = $("#name").val();
var standards_link = $("#standardslink");
var update_log = $("#update_log");
if(jQuery.inArray(standards_name, standards) != -1) {
if(companyid !== null) {
standards_link.attr("href", basic_href + "?standards_name=" + standards_name + "&company_id=" + companyid);
} else {
standards_link.attr("href", basic_href + "?standards_name=" + standards_name);
}
standards_link.css("color","#2EE52B");
} else if(standards_name == "") {
standards_link.removeAttr("href");
standards_link.css("color","black");
update_log.hide();
} else {
standards_link.removeAttr("href");
standards_link.css("color","#FF0011");
update_log.hide();
}
});但希望在autocomplete下拉列表中的每个选项上启动它。这就是我不能再深入的地方了。如果我手动设置变量"standards_name",例如var standards_name = "xxxx";,但我希望将ui.item.ticker_name值传递给该变量,则粘贴的代码可以正常工作。(“json_encode”数组和"companyid“是用PHP json_encode设置的,并且在脚本中前面传递过,运行正常)
代码如下:
$(function() {
$("#name").autocomplete({
source: "../autocomplete.php",
minLength: 2,
select: function(event, ui) {
$('#name').val(ui.item.ticker_name);
$('#mktcap').val(ui.item.mkt_cap);
var standards_name = ui.item.ticker_name;
var standards_link = $("#standardslink");
var update_log = $("#update_log");
if(jQuery.inArray(standards_name, standards) != -1) {
if(companyid !== null) {
standards_link.attr("href", basic_href + "?standards_name=" + standards_name + "&company_id=" + companyid);
} else {
standards_link.attr("href", basic_href + "?standards_name=" + standards_name);
}
standards_link.css("color","#2EE52B");
} else if(standards_name == "") {
standards_link.removeAttr("href");
standards_link.css("color","black");
update_log.hide();
} else {
standards_link.removeAttr("href");
standards_link.css("color","#FF0011");
update_log.hide();
}
}
});
}); 发布于 2012-07-17 20:58:35
最简单的解决方案是在select处理程序中添加
$("#name").attr("standards_name",ui.item.ticker_name);在第一个块中,您可以简单地
var standards_name = $("#name").attr("standards_name");更好的做法是使用jQuery数据函数保存所有对象:-)
发布于 2012-07-20 21:29:49
使用
$("#name").autocomplete({
source: function (request, response) {
/*Souse code here*/
},
select: function (e, i){
/*Selection code here inorder to get value use i.item.val*/
}
})https://stackoverflow.com/questions/11490931
复制相似问题