javascript - 在更改事件中获取单击的单选按钮 Id
问题描述
我有几组同名的单选按钮。
我想在更改事件中获取单击的单选按钮的 id。有几个与此问题相关的答案,但它们都返回选中的 id,但我想要的是单击的无线电 id。
更新 这是我的代码:
$('input[type=radio][name=SltOptionPane]').change(function () {
debugger;
//here i want to get the clicked id of the radio
var selectedValue = $("input[name=SltOptionPane]");
alert(selectedValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-6 col-sm-4 col-xs-12" data-toggle="buttons" id="26Panel">
<div class="col-md-6">
<label class="btn btn-chackbox checkbox-lable-styles active" style="margin-bottom: 5px;">
<span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="27" id="27" autocomplete="off"><text> طبيعي</text>
</label>
</div>
<div class="col-md-6">
<label class="btn btn-chackbox checkbox-lable-styles" style="margin-bottom: 5px;">
<span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="29" id="29" autocomplete="off"><text> ضعيف</text>
</label>
</div>
</div>
<div class="col-md-6 col-sm-4 col-xs-12" data-toggle="buttons" id="27Panel">
<div class="col-md-6">
<label class="btn btn-chackbox checkbox-lable-styles active" style="margin-bottom: 5px;">
<span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="30" id="30" autocomplete="off"><text> طبيعي</text>
</label>
</div>
<div class="col-md-6">
<label class="btn btn-chackbox checkbox-lable-styles" style="margin-bottom: 5px;">
<span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="31" id="31" autocomplete="off"><text> ضعيف</text>
</label>
</div>
</div>
请帮助,在此先感谢
解决方案
您应该使用它来引用当前元素。
尝试$(this).attr('id');
$('input[type=radio][name=SltOptionPane]').change(function () {
debugger;
//here i want to get the clicked id of the radio
var selectedValue = $(this).attr('id');
alert(selectedValue);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-6 col-sm-4 col-xs-12" data-toggle="buttons" id="26Panel">
<div class="col-md-6">
<label class="btn btn-chackbox checkbox-lable-styles active" style="margin-bottom: 5px;">
<span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="27" id="27" autocomplete="off"><text> طبيعي</text>
</label>
</div>
<div class="col-md-6">
<label class="btn btn-chackbox checkbox-lable-styles" style="margin-bottom: 5px;">
<span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="29" id="29" autocomplete="off"><text> ضعيف</text>
</label>
</div>
</div>
<div class="col-md-6 col-sm-4 col-xs-12" data-toggle="buttons" id="27Panel">
<div class="col-md-6">
<label class="btn btn-chackbox checkbox-lable-styles active" style="margin-bottom: 5px;">
<span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="30" id="30" autocomplete="off"><text> طبيعي</text>
</label>
</div>
<div class="col-md-6">
<label class="btn btn-chackbox checkbox-lable-styles" style="margin-bottom: 5px;">
<span class="glyphicon glyphicon-ok glyphicon-white"></span><input type="radio" name="SltOptionPane" value="31" id="31" autocomplete="off"><text> ضعيف</text>
</label>
</div>
</div>
推荐阅读
- java - 尝试运行 jar 文件时没有主要清单属性
- r - 在 R 中,以下代码之间有区别吗?df$age[df$age > 20] df[df$age>20, ]$age
- node-red - 以节点红色远程部署流文件而不覆盖整个流文件
- javascript - 滚动时计数器功能停止并显示错误
- swiftui - 让 SwiftUI Picker() 选择更新应用程序状态更改的最佳方法是什么?
- vb.net - vb.net 将复选框值插入 MS 服务器数据库
- python - Django模型,如何添加用户而不是实例?
- python - 在 Python 中求和 Counter 对象的有效方法
- html - 如何在图像中显示 Font Awesome 徽标
- docker - 如何在 containerd/ctr 中停止容器