2

href我有一个 JQuery 函数可以在添加或删除属性时更改按钮的颜色。如果我用模糊启动它,它会完美地工作

$("#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();
        } 
    });

但希望它在自动完成下拉列表中的选择上启动。这是我无法再进一步的地方。如果我手动设置变量“standards_name”,粘贴的代码就可以工作,例如,var standards_name = "xxxx";但我希望将ui.item.ticker_name值传递给变量。(“standards”数组和“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();
            } 
            }     

        });
});  
4

2 回答 2

4

最简单的解决方法是在选择处理程序中作弊添加

$("#name").attr("standards_name",ui.item.ticker_name);

在第一个块中,您可以简单地

  var standards_name = $("#name").attr("standards_name");

更好的是使用 jQuery Data 函数来保存所有对象:-)

于 2012-07-17T12:58:34.903 回答
1

利用

$("#name").autocomplete({
 source: function (request, response) {
/*Souse code here*/
},
select: function (e, i){
/*Selection code here inorder to get value use i.item.val*/

}
})
于 2012-07-20T13:29:49.310 回答