【问题标题】:foreach equivalent of php in jquery?foreach 相当于 jquery 中的 php?
【发布时间】:2010-11-14 23:13:51
【问题描述】:

在 JQuery 中是否有与 PHP 一样的 foreach 代码? 我在php中有一个代码,比如

<?php foreach ($viewfields as $viewfield): ?>       
if("<?php echo $viewfield['Attribute']['required'];?>"=='true'){
       $("<span class='req'><em> * </em></span>").appendTo("#fb_contentarea_col1down21 #label<?php echo $viewfield['Attribute']['sequence_no']?>");
   }


   if(<?=$viewfield['Attribute']['type'];?>=='text'||<?=$viewfield['Attribute']['type'];?>=='date'||<?=$viewfield['Attribute']['type'];?>=='number'){ 
       $("<input id=input<?=$viewfield['Attribute']['sequence_no'];?> type= 'text' style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> ></input><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
   }


   else if(<?=$viewfield['Attribute']['type'];?>=='textarea'){
       $("<textarea style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> id=input<?=$viewfield['Attribute']['sequence_no'];?>></textarea><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
}

在 Jquery 中是否有任何等效的 foreach?如何在 jQuery 中实现同样的功能?

编辑 1:

我认为它有效,但出现错误。代码和错误信息如下。

for(<?=$viewfield;?> in <?=$viewfields;?>){

 if("<?=$viewfield['Attribute']['required'];?>"=='true'){
            $("<span class='req'><em> * </em></span>").appendTo("#fb_contentarea_col1down21 #label<?php echo $viewfield['Attribute']['sequence_no']?>");
 }

 if(<?=$viewfield['Attribute']['type'];?>=='text'||<?=$viewfield['Attribute']['type'];?>=='date'||<?=$viewfield['Attribute']['type'];?>=='number'){ 
            $("<input id=input<?=$viewfield['Attribute']['sequence_no'];?> type= 'text' style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> ></input><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
 }

 else if(<?=$viewfield['Attribute']['type'];?>=='textarea'){
            $("<textarea style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> id=input<?=$viewfield['Attribute']['sequence_no'];?>></textarea><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
 }

}

错误信息:

语法错误 for(在数组中)

谁能帮帮我..

【问题讨论】:

  • JavaScript 和 PHP 无法交互... PHP 在发送到客户端之前创建 JavaScript 代码。发送到客户端后,执行。如果您查看源代码,您会看到 PHP 输出的是 for( in Array){(因为 $viewfield 是一个空变量,而 $viewfields 是一个 PHP 数组,当表示为字符串时显示为 Array。 ) 您需要全部使用 JavaScript 或全部使用 PHP,您不能混合使用这两种语言。
  • 但由于在 jquery 中使用 php,我在 document.ready() 关闭标记中收到错误消息。如果我为每个 php 代码删除它,错误就消失了,并且 document.location 工作正常。那么现在如何为 JQuery 中的每个代码编写呢?

标签: php jquery foreach


【解决方案1】:

$.each 功能类似。

它允许您使用可以访问每个项目的回调函数来迭代数组:

var arr = [ "one", "two", "three", "four", "five" ];


$.each(arr, function(index, value) {
  // work with value
});

了解一下可能很有用,如果您想打破循环,可以使用return false; 来完成,或者如果您只想跳过一次迭代(继续),您可以使用return true;

【讨论】:

    【解决方案2】:

    如果你想迭代一个对象,我会推荐 JavaScript 变体:

    for (var key in obj) {
        alert(key + ': ' + obj[key]);
    }
    

    也可以像这样在 jQuery 中迭代对象:
    注意!这样做毫无意义,除非您认为这种语法更易于维护。下面的语法比上面的标准 JavaScript for-loop 的开销要大得多。

    $.each(obj, function (key, value) {
        alert(key + ': ' + value);
    });
    

    要迭代数组,这就是您在标准 JavaScript 中的做法(假设 arr 是数组):

    for (var i = 0, l = arr.length; i < l; i++) {
        alert(i + ': ' + arr[i]);
    }
    

    要在 jQuery 中做,你可以这样做:

    $.each(arr, function (index, value) {
        alert(index + ': ' + value);
    });
    

    【讨论】:

    • 我可以使用 each 函数遍历数组的元素吗??
    • 是的,我现在添加了如何操作。
    【解决方案3】:

    jQuery.each

    【讨论】:

      【解决方案4】:

      Javascript 支持for(data in data_array) 语法。 jQuery 还有一个 $.each 函数(如前所述)

      【讨论】:

      • for-in 迭代对象的属性,而不是数组的元素。
      【解决方案5】:

      在选择器上运行的 Jquery:

      $('a').each(function() {
          $(this).click(function(e) {
             e.preventDefault()
             var href = this.href;
             open(href);
          });
          // operate on the anchor node.
      });
      

      jQuery 直接 $.each:

      var a = ['one', 'two'];
      
      $.each(a, function() {
          alert(this)
      });
      

      JS:普通 for 循环

       for ( var i = 0, len = 10; i<l; ++i ) {
          alert(i)
       }
      

      JS #2:原版的

       var humanLimbs = ['arms', 'legs'];
       for ( var limb in humanLimbs ) {
           if ( humanLimbs.hasOwnProperty(limb) ) {
              alert( limb )
           }
       }
      

      Js #3:无限循环

      for (;;) { alert(1) } // dont try this :p
      

      【讨论】:

        猜你喜欢
        • 2011-04-16
        • 2011-08-16
        • 1970-01-01
        • 2010-11-16
        • 2020-04-30
        • 2013-12-30
        • 1970-01-01
        • 2015-04-22
        • 1970-01-01
        相关资源
        最近更新 更多