[英]Passing checkbox value in array if checkbox is checked
My dynamically created checkbox code is here: 我动态创建的复选框代码在这里:
$(function(){
var currentUser = JSON.parse(window.localStorage.getItem('customer'));
$.ajax({
type : 'GET',
url : LiveUrl1 + "/api/Recent/GetAllRecent?userId="+currentUser.Id,
async : false,
beforeSend : function(){/*loading*/},
dataType : 'json',
success : function(result){
//console.log(result);
var buffer="";
$.each(result, function(index, val){
buffer+="<li class='ui-menu-item'><div id='ui-id-2' tabindex='-1' class='ui-menu-item-wrapper'><input type='checkbox' on-change='checkboxChanged' value="+val.Id+" style='margin-right:6px;' id='selectchkbox' class='selectchkbox' />"+val.SearchTerm+"</div></li>";
$("#Recent").html(buffer);
});
}
});
});
But, how do I pass this checkbox value in the array if the checkbox is checked? 但是,如果选中该复选框,如何在数组中传递此复选框值?
Here you can do like this: I did using jquery. 在这里你可以这样做:我使用了jquery。
$(function(){ var currentUser = JSON.parse(window.localStorage.getItem('customer')); $.ajax({ type : 'GET', url : LiveUrl1 + "/api/Recent/GetAllRecent?userId="+currentUser.Id, async : false, beforeSend : function(){/*loading*/}, dataType : 'json', success : function(result){ //console.log(result); var buffer=""; $.each(result, function(index, val){ buffer+="<li class='ui-menu-item'><div id='ui-id-2' tabindex='-1' class='ui-menu-item-wrapper'><input type='checkbox' on-change='checkboxChanged' value="+val.Id+" style='margin-right:6px;' id='selectchkbox' class='selectchkbox' />"+val.SearchTerm+"</div></li>"; $("#Recent").html(buffer); }); } }); var checkedValues = new Array(); $("input[type=checkbox]").on("change",function (){ var Value = $(this).val(); checkedValues.push(Value); console.log(Value); // To check the values on console. }); }); var checkedValues = new Array(); function checkboxChanged(id) { checkedValues.push(id); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Using javascript you can do this: 使用javascript你可以这样做:
$(function(){ var currentUser = JSON.parse(window.localStorage.getItem('customer')); $.ajax({ type : 'GET', url : LiveUrl1 + "/api/Recent/GetAllRecent?userId="+currentUser.Id, async : false, beforeSend : function(){/*loading*/}, dataType : 'json', success : function(result){ //console.log(result); var buffer=""; $.each(result, function(index, val){ buffer+="<li class='ui-menu-item'><div id='ui-id-2' tabindex='-1' class='ui-menu-item-wrapper'><input type='checkbox' on-change='checkboxChanged("+val.Id+")' value="+val.Id+" style='margin-right:6px;' id='selectchkbox' class='selectchkbox' />"+val.SearchTerm+"</div></li>"; $("#Recent").html(buffer); }); } }); var checkedValues = new Array(); var checkedValues = new Array(); function checkboxChanged(id) { checkedValues.push(id); console.log(id); } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.