1

我正在使用Select2 库v3.3,这是一种流行的基于 jQuery 的选择框替代品。

特别是,我正在使用对RottenTomatoes的 ajax 调用来测试该示例(请参阅此处的加载远程数据部分)。在那里,将隐藏输入放入 HTML 代码中,然后将 Select2 库附加到隐藏输入以呈现“自定义选择”。

为了使示例自包含,我在这里报告 HTML 的代码:

<input type="hidden" id="e6" style="width:600px"/>

以及对于 JS:

<script>
    $("#e6").select2({
        placeholder: "Search for a movie",
        minimumInputLength: 1,
        ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
            url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
            dataType: 'jsonp',
            data: function (term, page) {
                return {
                    q: term, // search term
                    page_limit: 10,
                    apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
                };
            },
            results: function (data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                return {results: data.movies};
            }
        },
        formatResult: movieFormatResult, // omitted for brevity, see the source of this page
        formatSelection: movieFormatSelection,  // omitted for brevity, see the source of this page
        dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
        escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
    });

  function movieFormatResult(movie){return movie.title;}
  function movieFormatSelection(movie){return movie.title;}
</script>

一旦选择了一个值,我如何从 jQuery 函数中删除这个值?

4

1 回答 1

1

以下几行应该有效:

function erase(){
    $("#e6").select2("data", null);
}

我知道,人们期望val应该为这个例子设置参数,但你必须使用它data

于 2013-02-08T10:08:34.960 回答