我正在尝试检索自定义属性“ mrbvalue”的值。在数据库中输入数据后,sharepoint给我的就是html。因此,我无法添加自己的课程或进行任何更改。 console.log()只是我在查看它是否有效。该代码有效,但是当我单击单选按钮的“否”和“有时”时,控制台将打印“是”值。
<tr>
<td> <span>P1</span> </td>
<td> <div class="n-mrb n-choice tbl" nfield="td_p1" value="1">
<div class="mrb-item"><label><input type="radio" name="td_p1" mrbvalue="Yes"> Yes</label></div>
<div class="mrb-item"><label><input type="radio" name="td_p1" mrbvalue="No"> No</label></div>
<div class="mrb-item"><label><input type="radio" name="td_p1" mrbvalue="Sometimes"> Sometimes</label></div>
</div>
</td>
</tr>
$(".mrb-item").click(function() {
var checkboxes = $(".mrb-item");
for(var i =0; i < checkboxes.length; i++){
if($('input').attr("mrbvalue") == "Yes" ){
console.log("yes");
}
}
if($('input').attr("mrbvalue") == "No" ){
console.log("No");
}
}
if($('input').attr("mrbvalue") == "Sometimes" ){
console.log("Sometimes");
}
}
})
最佳答案
修改代码如下。
<table>
<tr>
<td> <span>P1</span> </td>
<td> <div class="n-mrb n-choice tbl" nfield="td_p1" value="1">
<div class="mrb-item"><label><input type="radio" name="td_p1" mrbvalue="Yes"/> Yes</label></div>
<div class="mrb-item"><label><input type="radio" name="td_p1" mrbvalue="No"/> No</label></div>
<div class="mrb-item"><label><input type="radio" name="td_p1" mrbvalue="Sometimes"/> Sometimes</label></div>
</div>
</td>
</tr>
</table>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$(".mrb-item input").click(function() {
var mrbValue=$(this).attr("mrbvalue");
if(mrbValue== "Yes" ){
console.log("yes");
}
if(mrbValue== "No" ){
console.log("No");
}
if(mrbValue== "Sometimes" ){
console.log("Sometimes");
}
});
});
</script>