[英]Bootstrap switch - multiple switches on one page doesn't work
I have a problem when I have multiple switches on one page. 我在一页上有多个开关时遇到问题。 I'm using the bootstrap switch created by Mattia Larentis and Peter Stein. 我正在使用Mattia Larentis和Peter Stein创建的引导开关 。 I generate a datatable in my view like this: 我在我的视图中生成一个数据表,如下所示:
foreach($this->projects as $project){
foreach($project[$i] as $project_quiz){ ?>
<tr class="rowclick">
<td>10</td>
<td><a class="project_click" id="<?= $project["Id"] ?>"><?= $project["Name"] ?></a></td>
<td><a class="quiz_click" id="<?= $project_quiz["QuizId"] ?>" href="#" ><?= $project_quiz["Title"] ?></a></td>
<td>
<a href="<?php echo $this->url(array('controller'=>'quiz', 'action'=>'edit', 'quizid' => $project_quiz["QuizId"]), null, true);?>" class="no-js"><i class="icon-edit"></i></a>
<a href="<?php echo $this->url(array('controller'=>'quiz', 'action'=>'results', 'quizid' => $project_quiz["QuizId"]), null, true);?>" class="no-js"><i class="icon-bar-chart"></i></a>
<a href="<?php echo $this->url(array('controller'=>'quiz', 'action'=>'distribute', 'quizid' => $project_quiz["QuizId"]), null, true);?>" class="no-js"><i class="icon-rocket"></i></a>
<a href="<?php echo $this->url(array('controller'=>'quiz', 'action'=>'share', 'quizid' => $project_quiz["QuizId"]), null, true);?>" class="no-js"><i class="icon-share"></i></a>
<div id="label-toggle-switch" data-id="<?php echo $project_quiz["QuizId"] ?>" class="make-switch switch-small" data-on="default" data-on-label="Active" data-off-label="Inactive">
<input type="checkbox" checked>
</div>
</td>
</tr>
As you can see I have a bootstrap switch on every row of my table. 如您所见,我在表的每一行上都有一个引导开关。 This is my javascript according to the bootstrap switch: 根据引导开关,这是我的javascript:
$('#label-toggle-switch').on('switch-change', function (e, data) {
alert("test");
var quizid = $(this).attr('data-id');
var status = data.value; // TRUE OR FALSE
$.ajax({
url: '/overview/changestatus',
type:"POST",
data: {quizid: quizid, status: status},
success: function(data) {
console.log("ajax call succces");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
I have an alert box when you click on a switch, but the problem is this only works on the first switch in the first row of the datatable. 当您单击一个开关时,我有一个警告框,但问题是这仅适用于数据表第一行中的第一个开关。
Does someone knows what's wrong and how I can fix this? 有人知道哪里出了问题吗,我该如何解决?
您对所有switch元素都具有相同的id属性,但这不是有效的HTML,您应该使用通用类来定位元素,而不要使用id
$('.make-switch').on('switch-change', function (e, data) {...});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.