【发布时间】:2020-09-23 17:32:38
【问题描述】:
我试图在 php 循环中保留 3 个切换按钮,其中属性名称填充了动态值。
通过使用 Jquery onclick 事件,我想捕获标题属性。代码如下:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<link href="https://cdn.jsdelivr.net/css-toggle-switch/latest/toggle-switch.css" rel="stylesheet" />
<style>
.switch-toggle {
width: 10em;
}
.switch-toggle label:not(.disabled) {
cursor: pointer;
}
</style>
</head>
<body>
<?php for($i=0;$i<3;$i++){ ?>
<div class="switch-toggle switch-3 switch-candy">
<input id="on" name="state-d" type="radio" checked="" title="A-101-<?php echo $i; ?>">
<label for="on" onclick="">ON</label>
<input id="na" name="state-d" type="radio" disabled checked="checked">
<label for="na" onclick="">N/A</label>
<input id="off" name="state-d" type="radio" title="A-202-<?php echo $i; ?>">
<label for="off" onclick="">OFF</label>
<a></a>
</div>
<p></p>
<?php } ?>
</body>
<script>
$(function() {
$(".switch-candy input[type='radio']").click(function(){
var title = $(this).attr('title');
alert(title);
});
});
</script>
</html>
每当我点击任何按钮时,我只会获得第一个按钮属性。我无法获得第二个和第三个切换按钮的属性。
非常感谢任何建议。
谢谢
【问题讨论】:
-
你能附上渲染的html代码吗?我想说你删除 id attr。因为,您的代码会生成重复的 id。
-
@TomaszBucko,如果我删除了 id attr,则切换功能不起作用..
-
请检查我的答案。我尝试根据您的 php 代码构建 html 代码。并在镀铬检查控制台上进行测试。它正在工作。
-
对于 ids,我认为这并不重要。但同一页面中存在相同的 id 不是标准的。
-
@TomaszBucko,我需要在循环中而不是在 html 代码中,如果你有一个可行的想法,请告诉我。
标签: jquery toggle togglebutton