【问题标题】:Select2 plugin won't work properly inside while php loopSelect2插件在php循环中无法正常工作
【发布时间】:2018-03-07 15:23:27
【问题描述】:

我一直在尝试让 select2 插件工作,它在其他页面上运行良好,但是当我尝试在一段时间内运行它时,我只感到有些头疼,因为它只在最后一行工作。

这是我的“最小化”代码:

<?php
include("setting.php");
$pdosys=conectsys();
@session_start ();
?>

<html>
    <head>
        <title>Test Error Select2</title>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
        <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
    </head>
    <body>
        <h1>While</h1>
        <?php
        $searchid=$pdosys->prepare("SELECT * FROM tableclients WHERE id > 0");
        $searchid->execute();
        while($line=$searchid->fetch(PDO::FETCH_ASSOC)){
            ?>
            <a style="cursor:pointer;" data-toggle="modal" data-target="#myModal<?=$line[id]?>"><i class="material-icons">mode_edit</i></a><!-- Troggles modal -->
            <?php
            echo $line[id];
            echo "<br>";
            Modal($line[id]);//calls the function with modal but it only appears when the edit button is clicked
        }
        ?>
        <h1>End of while</h1>
    </body>
</html>

<?php
    function Modal($id) {//function with modal content that's determined by $id
        include_once("setting.php");
        $pdomodal=conectsys();
        ?>
        <div id="myModal<?=$id?>" class="modal fade" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button style="opacity: 1;" type="button" class="close" data-dismiss="modal"><i style="color:red;" class="material-icons">close</i></button>
                        <h4 class="modal-title">Edit</h4>
                    </div>
                    <div class="modal-body">
                        <script>//script that troggles select2
                            $(document).ready(function() {
                            $('.<?=$id?>').select2();
                            });
                        </script>
                        <!-- Select that select2 only work on the last row (the content still works on every row)-->
                        <select id="list1"  class="<?=$id?>">
                            <option value="">Choose the location</option>
                            <?php
                            $searchclients=$pdomodal->prepare("SELECT * FROM tableclients WHERE name = robert");
                            $searchclients->execute();
                            while($clients=$searchclients->fetch(PDO::FETCH_ASSOC)){
                                echo "<option value=".$clients["adress"].">".$clients["adress"]."</option>";
                            }
                            ?>
                        </select>
                        <br>
                    </div>
                </div>
            </div>
        </div>
<?php
    }
?>

$id 是模态变量,所以当我在 while 循环中调用它时可以获得多个内容,我尝试使用相同的逻辑来选择 select2,我从 select 类中获取值,但只有最后一个当我执行该代码时,行开始工作,可能我正在尝试使用的&lt;script&gt; 存在一些问题(只是从官方网站复制了示例),关于如何实现它的任何想法?

Obs:我检查了源代码,每个值都收到了正确的值,所以我确定问题出在我调用 select2 的方式上。

【问题讨论】:

  • 未定义常量 clients 那是你的错误
  • 是的,我的错误(已修复),但这不是问题所在。

标签: javascript php jquery jquery-select2


【解决方案1】:

这可能是因为您需要将脚本标签移动到它所引用的 id 之后。这只是一个猜测。选择二是棘手的。 jQuery 将递归地遍历与其选择器匹配的每个项目,因此您可以尝试使所有 id 相同并调用页面底部的函数。这完全是首选,因为代码就是代码,但我也建议查看Mustache Templating。这是一个额外的抽象层,在这些情况下可能会有所帮助。

【讨论】:

  • 我尝试了很多解决方案,包括在整个代码中移动脚本,但没有成功。
  • 你试过让你所有的id都一样吗?附: print 比 echo 快。
  • 如果我让所有的 id 相同,我不能让模态变化,所以我会得到所有结果的相同模态,这并不有趣,因为每个循环都会生成一个具有不同内容的模态。
【解决方案2】:

问题解决了,就像我认为问题出在select2 function 电话上一样,当我尝试这样做时:

 $(document).ready(function() {
     $('.<?=$id?>').select2();
 });

它使用循环中的最后一个值生成所有select2 onload,因此为了解决这个问题,我刚刚创建了一个具有不同名称的新函数(使用$id)并附加了它到我的代码上的 onclick 事件。所以最终的结果是这样的:

html

<input type='radio' required value="locate" onclick="select2<?=$id?>();">
<select id="list1"  class="<?=$id?>">
    <option>Test Location 1</option>
    <option>Test Location 2</option>
<select>

JS

function select2<?=$id?>(){
    $('.<?=$id?>').select2();
}

我的js技能真的很差,所以可能有更好的方法来调用它,但在我的情况下它很好。

【讨论】:

    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 2021-07-15
    • 2019-01-01
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多