Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试使用jquery获取元素并将其存储在数组中_Javascript_Jquery_Html - Fatal编程技术网

Javascript 尝试使用jquery获取元素并将其存储在数组中

Javascript 尝试使用jquery获取元素并将其存储在数组中,javascript,jquery,html,Javascript,Jquery,Html,我的html结构如下: <tr> <td>NC</td> <td><%= @subscriptions.where("users.ranking = 'NC'").count %></td> <td><input type="checkbox" name="my-checkbox" class="checkbox-ranking" id="bite" data-size='small

我的html结构如下:

<tr>
    <td>NC</td>
    <td><%= @subscriptions.where("users.ranking = 'NC'").count %></td>
    <td><input type="checkbox" name="my-checkbox" class="checkbox-ranking" id="bite" data-size='small'></td>
</tr>
<tr>
    <td>40</td>
    <td><%= @subscriptions.where("users.ranking = '40'").count %></td>
    <td><input type="checkbox" name="my-checkbox" class="checkbox-ranking" data-size='small' ></td>
</tr>
<tr>
    <td>30/5</td>
    <td><%= @subscriptions.where("users.ranking = '30/5'").count %></td>
    <td><input type="checkbox" name="my-checkbox" class="checkbox-ranking" data-size='small' ></td>
</tr>
我必须补充一点,我安装了gem引导开关,以具有开/关开关按钮,这就是我的复选框。我注意到我打开了按钮,它在html代码中没有传递到
checked
,所以我想这就是我的代码不起作用的原因:脚本实际上没有检测到on change事件

还有其他想法吗?

好的,现在可以了

$(document).ready(function(){
    $('.checkbox-ranking').on('change', function() {
        var rankings = $('.checkbox-ranking:checked').map(function() {
            return $(this).parent().parent().find('td:first-child').html();
        }).get().join(',');
        alert(rankings);  
    });
});
和jsfiddle:

好了,现在可以了

$(document).ready(function(){
    $('.checkbox-ranking').on('change', function() {
        var rankings = $('.checkbox-ranking:checked').map(function() {
            return $(this).parent().parent().find('td:first-child').html();
        }).get().join(',');
        alert(rankings);  
    });
});
和jsiddle:

尝试使用
closest()
first()

$(函数(){
$('input.checkbox ranking')。关于('change',函数(e){
var ranks=$('input.checkbox ranking:checked').map(函数(){
返回$(this).closest('tr').children().first().text();
}).get();
警报(等级.加入(','));
});
});

数控
40
30/5
尝试使用
最近的()
第一个()

$(函数(){
$('input.checkbox ranking')。关于('change',函数(e){
var ranks=$('input.checkbox ranking:checked').map(函数(){
返回$(this).closest('tr').children().first().text();
}).get();
警报(等级.加入(','));
});
});

数控
40
30/5

试试这个简单的提琴检查控制台是否有值这个提琴帮助?试试这个简单的提琴检查控制台是否有值这个提琴帮助?
parent().parent().parent()
可以简化为
parents().eq(2)
可以简化为
parents().parent().parent()
可以简化为
parents().eq(2)