首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将jquery ui自动完成选择传递给变量

将jquery ui自动完成选择传递给变量
EN

Stack Overflow用户
提问于 2012-07-15 17:44:09
回答 2查看 1.2K关注 0票数 2

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

代码语言:javascript
复制
$("#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设置的,并且在脚本中前面传递过,运行正常)

代码如下:

代码语言:javascript
复制
$(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();
            } 
            }     

        });
});  
EN

回答 2

Stack Overflow用户

发布于 2012-07-17 20:58:35

最简单的解决方案是在select处理程序中添加

代码语言:javascript
复制
$("#name").attr("standards_name",ui.item.ticker_name);

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

代码语言:javascript
复制
  var standards_name = $("#name").attr("standards_name");

更好的做法是使用jQuery数据函数保存所有对象:-)

票数 4
EN

Stack Overflow用户

发布于 2012-07-20 21:29:49

使用

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

}
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11490931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档