我正在尝试检索自定义属性“mrbvalue”的值。 html是我在数据库中输入数据后sharepoint给我的。所以我无法添加自己的类或更改任何内容。 console.log() 只是我看看它是否有效。该代码有效,但是当我单击单选按钮的“否”和“有时”时,控制台会打印"is"值。
<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>
关于jquery - jquery如何获取自定义属性的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59993092/