【问题标题】:how can i get attributes of textbox with typeahead of bootstrap in typeahead function call如何在 typeahead 函数调用中使用 bootstrap 的 typeahead 获取文本框的属性
【发布时间】:2013-02-26 05:26:11
【问题描述】:

我已经在一个文本框上使用了带有 ajax 的 bootstrap 预输入脚本,现在我进行了如下调用

$('#EquipmentLogEquipmentType').typeahead({
    source: function(query, process) {
        var $url =SITE_URL+ 'api/equiplg_typefields_typeahead/' + query + '.json';
        var $items = new Array;
        var id=$(this).attr('id');      
        id=id.split('eq_');
        id=id[1];           
        $items = [""];
        $.ajax({
            url: $url,
            dataType: "json",
            type: "POST",
            data: { field_name: id},
            success: function(data) {
                console.log(data);
                $.map(data, function(data){
                    var group;
                    group = {
                        id: data.id,
                        name: data.name,                            
                        toString: function () {
                            return JSON.stringify(this);
                            //return this.app;
                        },
                        toLowerCase: function () {
                            return this.name.toLowerCase();
                        },
                        indexOf: function (string) {
                            return String.prototype.indexOf.apply(this.name, arguments);
                        },
                        replace: function (string) {
                            var value = '';
                            value +=  this.name;
                            if(typeof(this.level) != 'undefined') {
                                value += ' <span class="pull-right muted">';
                                value += this.level;
                                value += '</span>';
                            }
                            return String.prototype.replace.apply('<div style="padding: 10px; font-size: 1.5em;">' + value + '</div>', arguments);
                        }
                    };
                    $items.push(group);
                });

                process($items);
            }
        });
    },
    property: 'name',
    items: 10,
    minLength: 2,
    updater: function (item) {
        var item = JSON.parse(item);
        console.log(item.name);         
        return item.name;
    }
});

现在在上面的代码中我提到了 $(this).attr('id');显示未定义...所以任何人都可以告诉我如何获取字段的属性,其中 typeahead 是获取目标?

提前谢谢:)

【问题讨论】:

  • 我不明白,你已经将事件绑定到#EquipmentLogEquipmentType,所以你实际上知道id。为什么不直接使用它?
  • 但我想像这样使用相同的功能:$('#eq_equipment_type,#eq_identifier,#eq_model,#eq_manufacturer').typeahead({ ....so 对于每个我不想通过条件静态代码...获取 id 值

标签: jquery cakephp twitter-bootstrap bootstrap-typeahead


【解决方案1】:

你必须使用

var id=this.$element.attr('id')); 



$('#eq_equipment_type,#eq_identifier,#eq_model,#eq_manufacturer').typeahead({
    source: function(query, process) {
        var $url =SITE_URL+ 'api/equiplg_typefields_typeahead/' + query + '.json';
        var $items = new Array;
        var id = this.$element.attr('id'));     
        id=id.split('eq_');
        id=id[1];           
        $items = [""];
        $.ajax({
            url: $url,
        ...

返回当前输入控件的正确 ID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多