标签: each

jQuery.each - 在ul中获取li元素

我的页面上有这个HTML:

<div class="phrase">
    <ul class="items">
        <li class="agap"><ul><li>TEXT1</li></ul></li>
        <li class="agap"><ul>  </ul></li> <!-- empty ul -->
        <li class="aword">TEXT2</li>
        ..
    </ul>
</div>

<div class="phrase"> ... </div>
Run Code Online (Sandbox Code Playgroud)

我想为每个"短语"获取文本变量中"items"中的所有元素,如下所示:

var string = "TEXT1 - BLANK - TEXT2";
Run Code Online (Sandbox Code Playgroud)

我目前有这个javascript代码:

<script>
$(function() {
    $('.phrase .items').each(function(){
        var myText = "";

        // At this point I need to loop all li items and get the text inside
        // depending on the class attribute

        alert(myText);

    });
};
</script>
Run Code Online (Sandbox Code Playgroud)

我怎么能<li>在里面迭代.items

我尝试了不同的方法但是没有得到好的结果.

each jquery

24
推荐指数
2
解决办法
17万
查看次数

如何在groovy的每个循环中使用"continue"

我是groovy的新手(在java上工作),尝试使用Spock框架编写一些测试用例.我需要使用"每个循环"将以下Java代码段转换为groovy代码段

Java代码段:

List<String> myList = Arrays.asList("Hello", "World!", "How", "Are", "You");
for( String myObj : myList){
    if(myObj==null) {
        continue;   // need to convert this part in groovy using each loop
    }
    System.out.println("My Object is "+ myObj);
}
Run Code Online (Sandbox Code Playgroud)

Groovy Snippet:

def myObj = ["Hello", "World!", "How", "Are", "You"]
myList.each{ myObj->
    if(myObj==null){
        //here I need to continue
    }
    println("My Object is " + myObj)
}
Run Code Online (Sandbox Code Playgroud)

each groovy for-loop continue spock

24
推荐指数
2
解决办法
2万
查看次数

使用Jquery each()循环通过输入字段进行验证

我正在制作一个表单,并且只有在输入值是数字时才会执行代码.我试图避免使用某种验证插件,并想知道是否有办法循环输入字段并检查值.

我一直在尝试以下但我认为我的逻辑错了:

(#monthlyincome是表单ID)

$("#submit").click(function() {

  $("#monthlyincome input").each(function() {

       if (!isNaN(this.value)) {
       // process stuff here

       }

   });

});
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

这是整个更新的代码:

$("#submit").click(function() {

    $("#monthlyincome input[type=text]").each(function() {
        if (!isNaN(this.value)) {
            // processing data      
            var age         = parseInt($("#age").val());
            var startingage = parseInt($("#startingage").val());

            if (startingage - age > 0) { 

                $("#field1").val(startingage - age);
                $("#field3").val($("#field1").val());

                var inflationyrs    = parseInt($("#field3").val());
                var inflationprc    = $("#field4").val() / 100;
                var inflationfactor = Math.pow(1 + inflationprc, inflationyrs);
                $("#field5").val(inflationfactor.toFixed(2)); 

                var estyearlyinc    = $("#field6").val();
                var inflatedyearlyinc = inflationfactor * estyearlyinc;
                $("#field7").val(FormatNumberBy3(inflatedyearlyinc.toFixed(0), ",", …
Run Code Online (Sandbox Code Playgroud)

forms validation each jquery input

23
推荐指数
1
解决办法
9万
查看次数

带有输入元素的jQuery .each()

//save tablet
jQuery("#savetablet"+jTablets[i].idtablets).on('click', function()
{
    alert("alertsepy2...");
    console.log(jTablets[i].idtablets);
    jQuery("#tablet"+jTablets[i].idtablets+" .detailsrow").each(function( index ) {
        $(this).each(function( index2 ) {
            console.log($(this).html());
        });
    });
});

<div class="column0"><input type="text" value="-D"></div>
<div class="column1"><input type="text" value="D"></div>
<div class="column2"><input type="text" value="D"></div>
<div class="column3"><input type="number" value="0"></div>
<div class="column4"> <input type="number" value="0"></div>
<div class="column5"> <input type="number" value="0"></div>
<div class="column6"><input type="number" value="0"></div>
<div class="column7"><input type="number" value="0"></div>
<div class="column8"><input type="number" value="0"></div>
<div class="column9"> <input type="number" value="0"></div>
<div class="column10"> <input type="number" value=""></div>
<div id="tablet17row0" class="column11">11</div>
<div class="column0"><input type="text" value="-D"></div>
<div class="column1"><input type="text" value="D"></div>
<div …
Run Code Online (Sandbox Code Playgroud)

each jquery input

23
推荐指数
2
解决办法
14万
查看次数

DRYest检查是否在Ruby/Rails中的.each循环的第一次迭代中的方法

在我.erb,我有一个简单的each循环:

<%= @user.settings.each do |s| %>
  ..
<% end %>
Run Code Online (Sandbox Code Playgroud)

检查它是否正在进行第一次迭代的最简单方法是什么?我知道我可以设置i=0...i++但是内部太乱了.erb.有什么建议?

each loops ruby-on-rails ruby-on-rails-3

21
推荐指数
2
解决办法
8874
查看次数

$(...).每个都不是函数

我正在尝试使用Web控制台将文本放在页面上的所有h2标记内.

我发现所有人都说要使用每一个,我试过了

var anArray = [];

$('h2').each( function(i,e) {
    anArray.push($(e).innerHTML);
});
Run Code Online (Sandbox Code Playgroud)

但它返回TypeError: $(...).each.each不是一个函数.

我也试过用

$.each('h2', function(i,e) {
        anArray.push($(e).innerHTML);
    });
Run Code Online (Sandbox Code Playgroud)

但是,我得到的TypeError: $.each只是一个功能?

each jquery typeerror

21
推荐指数
3
解决办法
3万
查看次数

javascript查找值是否为非IN数组

我的问题是即使对于重复的条形码,循环也会继续进入if语句.我正在尝试仅为唯一条形码输入if语句,但在循环结束时myArray中有重复项....为什么?

var myArray = new Array();  var i = 0;
$("li.foo").each(function(){
   var iBarCode = $(this).attr('barcode');
   if( !( iBarCode in myArray ) ){
      myArray[i++] = iBarCode;
      //do something else
   }
});
Run Code Online (Sandbox Code Playgroud)

javascript arrays each jquery

20
推荐指数
2
解决办法
6万
查看次数

jQuery.each实现与本机Array.forEach不同

有没有人为什么其他优秀的jQuery.each功能设计与(现在)本机不同Array.forEach?F.ex:

var arr = ['abc','def'];
arr.forEach(function(entry, index) {
    console.log(entry); // abc / def
});
Run Code Online (Sandbox Code Playgroud)

这绝对有道理.但jQuery选择将第index一个参数作为:

$.each(arr, function(index, entry) {
   console.log(entry);
});
Run Code Online (Sandbox Code Playgroud)

有谁知道这个设计决定背后的原因?我一直在$.each广泛使用,但它总是告诉我索引是第一个参数,因为它很少使用.我知道jQuery实现了直接引用,this但是如果你这样做会很困惑:

?var arr = ['abc','def'];
$.each(arr, function() {
    console.log(this === 'abc'); // false both times, since this is a String constructor
});?????????????????????????????
Run Code Online (Sandbox Code Playgroud)

并不是让我感到困扰,我更喜欢使用原生的polyfill来获得最常见的新数组函数,但我一直对设计决策感到好奇.也许它是在浏览器实现原生forEach和遗留支持之前的旧版本中制作的,无法阻止它们更改它,或者......?

或许,它是这样设计的,因为它也可以在本机对象上使用,然后将"键"放在回调值之前"有意义"?

旁注:我知道underscore.js(也许还有其他库)反过来(更类似于原生函数).

javascript api each foreach jquery

20
推荐指数
2
解决办法
4823
查看次数

jQuery - 循环使用具有特定属性的元素

我知道如何遍历下面的输入,搜索具有特定类"测试者"的输入

以下是我的表现方式:

<input type='text' name='firstname' class="testing" value='John'>
<input type='text' name='lastname' class="testing" value='Smith'>

<script type="text/javascript">
$(document).ready(function(){

 $.each($('.testing'), function() { 
   console.log($(this).val());
 });

});
</script>
Run Code Online (Sandbox Code Playgroud)

它按预期输出"John","Smith".

我想不使用class="testing"和使用自定义属性:testdata="John".

所以这就是我要做的事情:

<input type='text' name='firstname' testdata='John'>
<input type='text' name='lastname' testdata='Smith'>
Run Code Online (Sandbox Code Playgroud)

我的目标是使用内部的任何内容自动填充每个输入的值testdata,但只检测那些具有该testdata属性的值.

这是我尝试使用$.each循环的失败:

$.each($('input').attr('testdata'), function() {
  var testdata = $(this).attr('testdata');
  $(this).val(testdata);    
});
Run Code Online (Sandbox Code Playgroud)

我得到了这样的答复:Uncaught TypeError: Cannot read property 'length' of undefined 谁能看到我做错了什么?

each jquery custom-attributes

20
推荐指数
2
解决办法
5万
查看次数

为什么Array#每个都返回一个具有相同元素的数组?

我正在学习如何each在ruby中工作的细节,我尝试了以下代码行:

p [1,2,3,4,5].each { |element| el }
Run Code Online (Sandbox Code Playgroud)

结果是一个数组

[1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)

但我不认为我完全理解为什么.为什么each同一个数组的返回值?难道每个都不提供迭代方法吗?或者只是该each方法返回原始值的常见做法?

ruby arrays each self

19
推荐指数
2
解决办法
2万
查看次数