【发布时间】: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 类中获取值,但只有最后一个当我执行该代码时,行开始工作,可能我正在尝试使用的<script> 存在一些问题(只是从官方网站复制了示例),关于如何实现它的任何想法?
Obs:我检查了源代码,每个值都收到了正确的值,所以我确定问题出在我调用 select2 的方式上。
【问题讨论】:
-
未定义常量
clients那是你的错误 -
是的,我的错误(已修复),但这不是问题所在。
标签: javascript php jquery jquery-select2