简体   繁体   English

jQuery:多选单击事件不响应单击

[英]jQuery: multiselect click event doesn't respond to click

I have a multiselect box that contains a list of image urls. 我有一个多选框,其中包含图像网址列表。 I am trying to enable a click event on the multiselect box: when a user clicks on a url, it should pass the url to a textarea with the id of "articleFullText". 我试图在多选框上启用click事件:当用户点击url时,它应该将url传递给id为“articleFullText”的textarea。 Below is my jquery code, however, it doesn't work and does not cause any error on JS console: 下面是我的jquery代码,但它不起作用,并且不会在JS控制台上导致任何错误:

$('.multiselect').click(function() {
                    var src = $(this).val();
                    $('#articleFullText').val($('articleFullText').val() + src);
                });

My selectbox html: 我的选择框html:

<div class="controls">
                            <select name="images" class="multiselect" multiple="multiple">

                             <option value="http://localhost/images/1.jpg">http://localhost/images/1.jpg</option>
<option value="http://localhost/images/2.jpg">http://localhost/images/2.jpg</option>

                            </select>
                        </div>

textarea code: textarea代码:

<textarea class="form-control" rows="3" name="articleFullText" id="articleFullText></textarea>

Two mistakes: http://jsfiddle.net/TrueBlueAussie/dGa97/1/ 两个错误: http//jsfiddle.net/TrueBlueAussie/dGa97/1/

A missing # selector: 一个缺少的#选择器:

$('.multiselect').click(function () {
    var src = $(this).val() +"blah";
    $('#articleFullText').val($('#articleFullText').val() + src);
});

And a missing closing quote on id="articleFullText" : 并且在id="articleFullText"上缺少关闭引号:

<textarea class="form-control" rows="3" name="articleFullText" id="articleFullText"></textarea>

You are missin the # on the selector: 你错过了选择器上的#:

$('#articleFullText').val($('articleFullText').val() + src);

change to: 改成:

$('#articleFullText').val($('#articleFullText').val() + src);

And the Id in the textarea has not a closing "" change it to: 并且textarea中的Id没有关闭""将其更改为:

<textarea class="form-control" rows="3" name="articleFullText" id="articleFullText"></textarea>

DEMO DEMO

You didn't use the right event (change). 您没有使用正确的事件(更改)。

$('.multiselect').on('change', function() {
  $('#articleFullText').append($(this).val())
});

Here is a working fiddle : http://jsfiddle.net/9Ya27/1/ 这是一个工作小提琴: http//jsfiddle.net/9Ya27/1/

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

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