简体   繁体   English

PHP如何在Javascript中使用Foreach循环

[英]PHP how to use Foreach loop in Javascript

I want a teacher to select multiple classes when he click a button, the classes are selected from the database and displayed via an tag. 我希望老师单击按钮时选择多个班级,这些班级是从数据库中选择的,并通过标签显示。 I have made a function which makes a new select and displays another list of the classes, however my foreach loop doesn't seem to work... 我做了一个可以进行新选择并显示其他类列表的函数,但是我的foreach循环似乎不起作用...

Here's my JQuery which is inside a 'script' tag in the HTML: 这是我的JQuery,它位于HTML的“脚本”标记中:

$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><select name="klas" style="width:40%;"><?php foreach ($klas as $klas) {echo "<option  value='".$klas->getCode()."'>".$klas->getCode()."</option>"; } ?></select><a href="#" class="remove_field">Verwijder</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

This is the button and the corresponding div's: 这是按钮和对应的div:

<div class="input_fields_wrap">
    <button type="button" class="add_field_button">Add More Fields</button>
    <div>
<select name="klas">
<?php foreach ($klas as $klas) {
echo "<option  value='".$klas->getCode()."'>".$klas->getCode()."</option>"; } ?>
</select>
    </div>
</div>

Does someone have any idea how I can achieve the same result from JQuery as from the regular php/html select box? 有人知道我如何从JQuery中获得与常规php / html选择框相同的结果吗?

put your array in arr format in this answer and append to the div wherever you want on page currently i use select_div 将您的数组以arr格式显示在此答案中,并附加到div,无论您当前在页面上的哪个位置,我都使用select_div

<div class="input_fields_wrap">
        <button type="button" class="add_field_button">Add More Fields</button>
        <div id ="select_div">
        </div>
    </div>
    <script type="text/javascript">
    var arr = [
      {val : 1, text: 'One'},
      {val : 2, text: 'Two'},
      {val : 3, text: 'Three'}
    ];

    var sel = $('<select>').appendTo('#select_div');

    $(arr).each(function() {
     sel.append($("<option>").attr('value',this.val).text(this.text));
    });
    sel.append("</select>")
    </script>

Found it. 找到了。 Your problem is that you use the same value name for both $klas array as well as internal foreach variable. 您的问题是$klas数组和内部foreach变量都使用相同的值名称。 While this is not technically incorrect, notice that after foreach $klas variable will be equal to last element of the array - not the array itself . 尽管从技术上来讲这不是错误的,但是请注意,在foreach $klas变量将等于数组的最后一个元素,而不是数组本身

Since you were using this loop inside javascript it was tough to spot, and local test with foreach showed that everything is fine. 由于您是在javascript中使用此循环,因此很难发现,并且使用foreach进行本地测试表明一切都很好。

Anyway please update your code (everywhere were it is used) from: 无论如何,请从以下位置更新您的代码(无论在何处使用):

<?php 
    foreach ($klas as $klas) { 
        $klas->getCode()
    }
?>

to

<?php 
    foreach ($klas as $k) {
        $k->getCode();
    }
 ?>

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

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