我在 PHP 循环中有以下单选按钮:
<form>
<table class="table table-bordered optionsChk">
<tr>
<td colspan="2"> Select Bench for case </td>
</tr>
<?php foreach($db->getRecordSet($sql) as $bench){ ;?>
<tr>
<td class="w5">
<input type="radio" alt="<?php echo($bench['lbl']); ?>" value="<?php echo($bench['sno']); ?>" name="b_sno" id="b_<?php echo($bench['sno']); ?>">
</td>
<td class="w95"> <label for="b_<?php echo($bench['sno']); ?>"><?php echo($bench['lbl']); ?></label> </td>
</tr>
<?php
}
?>
我用以下函数显示上面的值:
function doneBenchSelection() {
try {
var benchSno = $("input[type=radio][name=b_sno]:checked").val();
$("#benchSno").val(benchSno);
$("#obj_lst").html("Selected Bench Label: "+$("input[type=radio][name=b_sno]:checked").text());
} catch(e) {
alert(e.message);
}
}
我想从选定的单选按钮中获取 alt="some text"
文本:
$("#obj_lst").html("Selected Bench Label: "+$("input[type=radio][name=b_sno]:checked").text());
这是正确和推荐的方式吗?
最佳答案
使用 attr功能
$('input[type="radio"]:checked').attr('alt');
如果您有多个具有不同名称的单选按钮,则使用循环:
labels = [];
$('input[type="radio"]:checked').each(function(){
labels.push($(this).attr('alt'));
});
关于javascript - 使用 jQuery 从选定的单选按钮获取 Alt =""值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521419/