使用Javascript获取复选框的自定义属性

时间:2017-05-11 12:03:18

标签: javascript jquery html events

我如何获得自定义属性" mem_name"价值?

<input class="messageCheckbox" type="checkbox" value="3"  mem_name='ABC' name="checkmember" >
<input class="messageCheckbox" type="checkbox" value="1"  mem_name='PQR' name="checkmember" >

   <input type="button" onclick="resend_invite">

 function resend_invite() {
    if ($('input[name="checkmember"]:checked').length > 0) {
        if (confirm('Are you sure you want to send invite to selected Member?')) {
            var checkboxes = document.getElementsByName('checkmember');
            var vals = "";
            for (var i = 0, n = checkboxes.length; i < n; i++) {
                if (checkboxes[i].checked)
                {
                    vals += checkboxes[i].$(value).attr('mem_id');
                    vals += ",";
                }
            }
            alert(vals);
            return false;
            var vals = vals.replace(/,\s*$/, '');
            var vals = vals.replace(/,\s*$/, '');
            var valsArray = new Array();
            valsArray = vals.split(",");
        }
    } else {
        alert('Please select at least one Member');
        return false;
    }
}

我想输出像ABC,PQR 我使用&#34; vals + = checkboxes [i]。$(value).attr(&#39; mem_id&#39;);&#34;
但它没有用。

3 个答案:

答案 0 :(得分:1)

您应该使用$(checkboxes[i])来获取与其operationsproperties关联的jquery对象。

$(checkboxes[i]).attr('mem_name')

function resend_invite() {
   if ($('input[name="checkmember"]:checked').length > 0) {
        if (confirm('Are you sure you want to send invite to selected Member?')) {
            var checkboxes = document.getElementsByName('checkmember');
            var vals = "";
            for (var i = 0, n = checkboxes.length; i < n; i++) {
                if (checkboxes[i].checked)
                {
                    vals += $(checkboxes[i]).attr('mem_name');
                    vals += ",";
                }
            }
            alert(vals);
            return false;
            var vals = vals.replace(/,\s*$/, '');
            var vals = vals.replace(/,\s*$/, '');
            var valsArray = new Array();
            valsArray = vals.split(",");
        }
    } else {
        alert('Please select at least one Member');
        return false;
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="messageCheckbox" type="checkbox" value="3"  mem_name='ABC' name="checkmember" >
<input class="messageCheckbox" type="checkbox" value="1"  mem_name='PQR' name="checkmember" >

   <input type="button" onclick="resend_invite()" value="Resend">

您可以使用javascript方法使用纯getAttribute执行此操作。

function resend_invite() {
   if ($('input[name="checkmember"]:checked').length > 0) {
        if (confirm('Are you sure you want to send invite to selected Member?')) {
            var checkboxes = document.getElementsByName('checkmember');
            var vals = "";
            for (var i = 0, n = checkboxes.length; i < n; i++) {
                if (checkboxes[i].checked)
                {
                    vals += checkboxes[i].getAttribute('mem_name');
                    vals += ",";
                }
            }
            alert(vals);
            return false;
            var vals = vals.replace(/,\s*$/, '');
            var vals = vals.replace(/,\s*$/, '');
            var valsArray = new Array();
            valsArray = vals.split(",");
        }
    } else {
        alert('Please select at least one Member');
        return false;
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="messageCheckbox" type="checkbox" value="3"  mem_name='ABC' name="checkmember" >
<input class="messageCheckbox" type="checkbox" value="1"  mem_name='PQR' name="checkmember" >

   <input type="button" onclick="resend_invite()" value="Resend">

答案 1 :(得分:1)

变化

vals += checkboxes[i].$(value).attr('mem_id');

vals += $(checkboxes[i]).attr('mem_id');

答案 2 :(得分:1)

请试试这个小提琴。希望它有效。 Fiddle

&#13;
&#13;
$(document).ready(function(){
	$("#memAdd").click(function(){
  if ($('input[name="checkmember"]:checked').length > 0) {
        if (confirm('Are you sure you want to send invite to selected Member?')) {
            var checkboxes = document.getElementsByName('checkmember');
            var vals = [];
            for (var i = 0, n = checkboxes.length; i < n; i++) {
                if (checkboxes[i].checked)
                {
                    vals += $(checkboxes[i]).attr('mem_name');
                    vals += ",";
                }
            }
            alert(vals);
            return false;
            var vals = vals.replace(/,\s*$/, '');
            var vals = vals.replace(/,\s*$/, '');
            var valsArray = new Array();
            valsArray = vals.split(",");
        }
    } else {
        alert('Please select at least one Member');
        return false;
    }
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="messageCheckbox" type="checkbox" value="3"  mem_name='ABC' name="checkmember" >
<input class="messageCheckbox" type="checkbox" value="1"  mem_name='PQR' name="checkmember" >

   <input type="button" id="memAdd"  value="memAdd">
&#13;
&#13;
&#13;