jQuery更改检测值

时间:2013-07-22 16:17:18

标签: jquery jquery-tokeninput

我使用以下教程从json列表填充下拉菜单:http://loopj.com/jquery-tokeninput/

我特别使用以下代码:

    $(document).ready(function() {
        $("#demo-input-local").tokenInput([
            {name: "Ruby"},
            {name: "Python"},
            {name: "JavaScript"},
            {name: "ActionScript"},
            {name: "Scheme"},
            {name: "Lisp"},
            {name: "C#"},
            {name: "Fortran"},
            {name: "Visual Basic"},
            {name: "C"},
            {name: "C++"},
            {name: "Java"},
            {name: "Taranto"}
        ]);

        $("#demo-input-local").change(function(){
            var city = // get the selected value on change
            console.log(city);
        });

    });

我希望能够在选择新选项后立即选择值

2 个答案:

答案 0 :(得分:0)

this.value保存所选项目的值。

答案 1 :(得分:0)

使用:

$("#demo-input-local").change(function(){
        var city = $(this).val();
        console.log(city);
});

演示:http://jsfiddle.net/FlameTrap/uAhGJ/