首页 \ 问答 \ Rails 3:乘以一个数组的所有元素(Ruby: Multiply all elements of an array)

Rails 3:乘以一个数组的所有元素(Ruby: Multiply all elements of an array)

假设我有一个数组A = [1,2,3,4,5]

我如何将所有元素与ruby相乘并得到结果? 1 * 2 * 3 * 4 * 5 = 120

以及如果有一个元素0呢? 我怎样才能忽略这个元素?


Let's say I have an array A = [1, 2, 3, 4, 5]

how can I multiply all elements with ruby and get the result? 1*2*3*4*5 = 120

and what if there is an element 0 ? How can I ignore this element?


原文:https://stackoverflow.com/questions/7050869
更新时间:2022-11-06 15:11

最满意答案

尝试这样的事情:

$(function(){
  $('li').click(function(){
    $('#parentLi').find('input[type="checkbox"]').prop('checked', true);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<li id="parentLi">
    <span class="collapsible"></span>
    <span>System</span>
    <ul>
        <li>
            <span class="collapsible"></span>
            <span>Division</span>
            <ul>
                <li>
                    <input class="chkGroup" type="checkbox">
                </li>
            </ul>
        </li>
    </ul>
</li>


Try something like this:

$(function(){
  $('li').click(function(){
    $('#parentLi').find('input[type="checkbox"]').prop('checked', true);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<li id="parentLi">
    <span class="collapsible"></span>
    <span>System</span>
    <ul>
        <li>
            <span class="collapsible"></span>
            <span>Division</span>
            <ul>
                <li>
                    <input class="chkGroup" type="checkbox">
                </li>
            </ul>
        </li>
    </ul>
</li>

相关问答

更多
  • 尝试这个: $(document).ready(function(){ $('.check:button').toggle(function(){ $('input:checkbox').attr('checked','checked'); $(this).val('uncheck all'); },function(){ $('input:checkbox').removeAttr('checked'); $(this).va ...
  • 对于jQuery <1.6: 要检查/取消选中复选框,请使用checked和更改的属性。 使用jQuery可以做到: $('#myCheckbox').attr('checked', true); // Checks it $('#myCheckbox').attr('checked', false); // Unchecks it 因为你知道,在HTML中,它看起来像: