admin管理员组

文章数量:1318328

I'm using selectize.js:

  • I have a number of similar select boxes
  • Options are loaded dynamically from the server, during load()
  • The query made during .load() is unique to each select box. Eg, I'd like to have a .load() hit a different URL based on some property of the select box (eg, data-someproperty).

How can I do that? Code right now is almost identical to the Remote Source example from the Selectize docs.

$('.select-repo').selectize({
    valueField: 'url',
    labelField: 'name',
    searchField: 'name',
    create: false,
    ...
    load: function(query, callback) {
        if (!query.length) return callback();
        $.ajax({
            url: '/' + encodeURIComponent(query),
            type: 'GET',
            error: function() {
                callback();
            },
            success: function(res) {
                callback(res.repositories.slice(0, 10));
            }
        });
    }
});

I tried checking out this during .load() but I can't see any reference to the original element.

I'm using selectize.js:

  • I have a number of similar select boxes
  • Options are loaded dynamically from the server, during load()
  • The query made during .load() is unique to each select box. Eg, I'd like to have a .load() hit a different URL based on some property of the select box (eg, data-someproperty).

How can I do that? Code right now is almost identical to the Remote Source example from the Selectize docs.

$('.select-repo').selectize({
    valueField: 'url',
    labelField: 'name',
    searchField: 'name',
    create: false,
    ...
    load: function(query, callback) {
        if (!query.length) return callback();
        $.ajax({
            url: 'https://api.github./legacy/repos/search/' + encodeURIComponent(query),
            type: 'GET',
            error: function() {
                callback();
            },
            success: function(res) {
                callback(res.repositories.slice(0, 10));
            }
        });
    }
});

I tried checking out this during .load() but I can't see any reference to the original element.

Share Improve this question edited Apr 19, 2016 at 10:35 mikemaccana asked Jan 17, 2014 at 15:24 mikemaccanamikemaccana 124k110 gold badges430 silver badges533 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Found it:

Inside load(), access:

this.$input

To get a JQuery selection with the <select> element.

this.$input.data('whatever')

Would return data-whatever from the select

本文标签: javascriptselectizejshow can reference some property of the select element during load()Stack Overflow