繁体   English   中英

JavaScript中的PHP foreach循环

[英]PHP foreach loop in Javascript

如何在Javascript中进行这样的循环?

foreach ($viewData['cure'] as $cure) 

在此循环中,我想在JS中打印结果

$("#new").append('<label for="nice_text">ID Type</label><input type="text" id="nice_text" name="cureIdtype" class="input-text" VALUE="<?php echo (string)$cure["id_type"] ?>"/>');

您可以像这样简单地将两者结合

<script type="text/javascript">
<?php foreach ($viewData['cure'] as $cure): ?>
$("#new").append('<label for="nice_text<?php echo $cure["id"] ?>">ID Type</label><input type="text" id="nice_text<?php echo $cure["id"] ?>"/>" name="cureIdtype" class="input-text" value="<?php echo (string)$cure["id_type"] ?>"/>');
<?php endforeach; ?>
</script>

只需确保您的ID是唯一的即可(我$cure["id"]为例),您可能还想更改name="cureIdtype" ,这取决于您随后要对输入执行的操作。

我自己仔细检查了一下,看看是否有比我下面更优雅的东西,并且没有提到我的解决方案。

的PHP

foreach ( $viewData['cure'] as $cure ) {
    ?>
    // your code from above
    $("#new").append('<label for="nice_text">ID Type</label><input type="text" id="nice_text" name="cureIdtype" class="input-text" value="<?php echo $cure["id_type"]; ?>"/>');
    <?php
}

Java脚本

var cures = <?php echo json_encode( $viewData['cure'] ); ?>;
for ( var key in cures ) {
    var cure = cures[key];
    // your code from above modified to use the Javascript variable created
    $("#new").append('<label for="nice_text">ID Type</label><input type="text" id="nice_text" name="cureIdtype" class="input-text" value="'+cure["id_type"]+'"/>');
}

Javascript没有像PHP那样的foreach ,您需要使用常规的for循环。

var a = ["a", "b", "c"];
for (var index = 0; index < a.length; ++index) {
    console.log(a[index]);
}

(来自https://stackoverflow.com/questions/9329446/for-each-in-an-array-how-to-do-that-in-javascript

如果要使用PHP进行此操作并将内容回显到浏览器,则外观如下所示:

foreach ($viewData['cure'] as $cure) {
    echo '$("#new").append("<label for=\'nice_text\'>ID Type</label><input type=\'text\' id=\'nice_text\' name=\'cureIdtype\' class=\'input-text\' VALUE=\'' . (string)$cure['id_type'] . '\'/>")';
}

如果您有一个JavaScript数组并想在那里处理它,它将看起来像这样:

for (var i = 0; i < viewData['cure'].length; ++i) {
    $("#new").append('<label for="nice_text">ID Type</label><input type="text" id="nice_text" name="cureIdtype" class="input-text" VALUE="' + viewData['cure'][i]['id_type'] + '"/>")';
}

这些示例未经测试,到处都是单引号/双引号和转义符,因此我可以在其中输入一些错字。

Javascript数组forEach()方法


描述:

Javascript数组forEach()方法为数组中的每个元素调用一个函数。

句法:

array.forEach(callback[, thisObject]);

以下是参数的详细信息:

  • callback:用于测试数组中每个元素的函数。
  • thisObject:执行回调时用作此对象的对象。

返回值:

返回创建的数组。

兼容性:

此方法是ECMA-262标准的JavaScript扩展; 因此,它可能不会在该标准的其他实现中出现。 为了使其正常工作,您需要在脚本顶部添加以下代码:

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

例:

<html>
<head>
<title>JavaScript Array forEach Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

function printBr(element, index, array) {
  document.write("<br />[" + index + "] is " + element ); 
}

[12, 5, 8, 130, 44].forEach(printBr);

</script>
</body>
</html>

这将产生以下结果:

[0] is 12
[1] is 5
[2] is 8
[3] is 130
[4] is 44 

为了更好地理解它,您可以自己尝试

资源

var a = ["a", "b", "c", "d"];
a.forEach(function(data) {
    console.log(data);
});

我不明白您的代码有什么问题...

<script>
var array = [];
<? foreach ($viewData['cure'] as $cure) {?>
    array.push(<? echo $cure?>);
<? }?>
array.forEach(function(value){
    $("#new").append('<label for="nice_text">ID Type</label><input type="text" id="nice_text" name="cureIdtype" class="input-text" VALUE="'+value+'"/>')
});

</script>

以下是您可以使用Javascript编写或使用forEach的方法之一。

 const arr = ['value0', 'value1', 'value2']; arr.forEach(element => { console.log(element); }); 

您也可以参考此链接 ,以帮助您了解javascript中for和forEach之间的区别。

 const arr = ['value0', 'value1', 'value2']; for (let i in arr) { console.log(arr[i]) } 

暂无
暂无

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

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