简体   繁体   English

提醒选定的jQuery下拉列表中的文本

[英]Alerting the text that is inside chosen jquery dropdown select

I am using jquery chosen , with the specific component multiple select . 我正在使用jquery selected ,其特定组件为select

I want to listen if something is typed in the chosen text bar and alert the content typed in that. 我想听听是否在选定的文本栏中键入了某些内容,并提醒在其中键入的内容。

To achieve that I have: 为了实现这一点,我有:

HTML: HTML:

<select id="searchZip" name="types[]" multiple class="searchZip chosen-select-no-results" tabindex="12">
</select>

JS: JS:

<script type="text/javascript">
    var config = {
        '.chosen-select': {},
        '.chosen-select-deselect': {
            allow_single_deselect: true
        },
        '.chosen-select-no-single': {
            disable_search_threshold: 10
        },
        '.chosen-select-no-results': {
            no_results_text: 'Oops, nothing found!'
        },
        '.chosen-select-width': {
            width: "95%"
        }
    }
    for (var selector in config) {
        $(selector).chosen(config[selector]);
    }
</script>

I want to call alert() if something is typed in the search box. 如果要在搜索框中键入内容,我想致电alert() How can I do that? 我怎样才能做到这一点?

$("#searchZip").chosen().change( alert("a"); );

When I do this, I always get this error: 当我这样做时,我总是会收到此错误:

Uncaught SyntaxError: missing ) after argument list 参数列表后未捕获到的SyntaxError:缺少)

How can I call alert with the input that is typed in the chosen input? 如何使用在所选输入中键入的输入来调用警报?

Inside the alert box there should not be any semicolon as it terminates the statement.. 在警报框中,终止语句时不应使用任何分号。

just write .change(alert("a")); 只需写.change(alert("a"));

You need to change the following line from this: 您需要从此更改以下行:

$("#searchZip").chosen().change( alert("a"); );

To this: 对此:

$("#searchZip").chosen().change( alert("a") );

When JavaScript sees a ; 当JavaScript看到; , it assumes it is the end of the line. ,它假设这是行的结尾。 Since your ; 既然你; was before inside the .change() , JavaScript did not see a closing ) for .change() , so it producedthe following error stating that it was missing ) after argument list 是之前内部.change() ,JavaScript的没有看到关闭).change()所以它producedthe以下错误,指出它missing ) after argument list

Uncaught SyntaxError: missing ) after argument list

That line passed to function .change() should be a function. 传递给函数.change()的那一行应该是一个函数。 alert("a") is a function invocation (statement). alert("a")是一个函数调用(语句)。 That invocation could be moved into lambda (anonymous) function: 该调用可以移到lambda(匿名)函数中:

$("#searchZip").chosen().change( function() {alert("a"); });

Or a named function could be used: 或者可以使用命名函数:

function alertA() {
    alert("a");
}
$("#searchZip").chosen().change( alertA );

For more information, refer to the MDN documentation on functions as callbacks . 有关更多信息,请参阅有关将函数用作回调MDN文档

The " documentation " is a little lacking as far as the arguments passed to .change() , but change calls .trigger() so the arguments are: 就传递给.change()的参数而言,“ 文档 ”有点缺乏,但是change调用了.trigger(),因此参数为:

  • event : event 事件事件

    contains information about the event 包含有关事件的信息

  • extraParam : Object extraParam对象

    contains information about the selected option 包含有关所选选项的信息

The extraParam can be used to get the text of the selected option: extraParam可用于获取所选选项的文本:

function alertA(event, result) {
    alert(result.selected);
}

See a demonstration of this below. 请参阅下面的演示。

 $('.chosen-select-no-results').chosen({ no_results_text: 'Oops, nothing found!' }); function alertA(event, result) { alert(result.selected); } $("#searchZip").chosen().change(alertA); 
 <link href="https://harvesthq.github.io/chosen/chosen.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script src="https://harvesthq.github.io/chosen/chosen.jquery.js"></script> <select id="searchZip" name="types[]" multiple class="searchZip chosen-select-no-results" tabindex="12"> <option>Cat</option> <option>Dog</option> <option>Zebra</option> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM