简体   繁体   English

复选框上的click事件不会触发

[英]click event on checkbox will not fire

Here's the markup: 这是标记:

 $('.clsEvent input[type=checkbox]').change(function(e) { debugger; var title = $(e).val(); }); This is the function that builds the list and is called in the document ready function: function popServiceUserEvents(e) { $.ajax({ type: "POST", url: "@Url.Action("GetServiceUserEvents")/" + e, success: function(data) { if (data.events.length > 0) { for (var i = 0; i < data.events.length; i++) { $(".ulEvent") .append("<li> <input class='clsEvent' type='checkbox' value='" + data.events[i].Value + "'/> &nbsp;" + data.events[i].Text + "</li>"); } } }, error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="multiEvent"> <ul class="ulEvent" style="display: block;"> <li> <input class="clsEvent" type="checkbox" value="3"> &nbsp;Event 1</li> <li> <input class="clsEvent" type="checkbox" value="4"> &nbsp;Event 2</li> </ul> </div> 

it doesn't matter if I change the event to click or change it to be in the form ...).on('change',function(... or if I just attempt the event against the class name without the type. 不管我将事件更改为单击还是将其更改为...)。on('change',function(...还是我只是针对没有类型的类名尝试事件。

The page before I build the list is defined with this markup: 我在建立列表之前的页面是用以下标记定义的:

<div class="row eventDrop">
    <div class="col-md-6">
        <dl class="myDropdown">
            <dt>
                <a href="#" style="color: #fff; font-weight: normal;">
                    <span class="topSection">Event</span>
                    <p class="multiSel"></p>
                </a>
            </dt>
            <dd>
                <div class="multiEvent">
                    <ul class="ulEvent"></ul>
                </div>
            </dd>

        </dl>
    </div>

Wrong Selector 选择器错误

Your selector is wrong, you are searching a checkbox inside an element with clsEvent class 您的选择器错误,您正在使用clsEvent类搜索元素内的复选框

Those selectors are right choose the one you want ;): 这些选择器是正确选择您想要的一个;):

.clsEvent

input[type=checkbox].clsEvent

.ulEvent input[type=checkbox]

Trying to access .val() from Event Object 尝试从Event Object访问.val()

Moreover e is an Event Object , your element is $(e.target) 而且e是一个Event Object ,您的元素是$(e.target)

 $('input[type=checkbox].clsEvent').change(function(e) { var title = $(e.target).val(); console.log(title); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="multiEvent"> <ul class="ulEvent" style="display: block;"> <li> <input class="clsEvent" type="checkbox" value="3"> &nbsp;Event 1</li> <li> <input class="clsEvent" type="checkbox" value="4"> &nbsp;Event 2</li> </ul> </div> 

It's because your selector is wrong. 这是因为您的选择器有误。

Try below : 请尝试以下方法:

 $('input[type=checkbox].clsEvent').change(function(e) {
    debugger;
    var title = $(e).val();

});

You are trying to get the input inside the class .clsEvent and therefore not finding anything as your inputs have the class clsEvent. 您正在试图获取类.clsEvent 内部的输入,因此没有找到任何东西作为你的输入具有类clsEvent。

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

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