![](/img/trans.png)
[英]The value of the checkbox is added to the array but the checkbox is not checked
[英]Passing checkbox value in array if checkbox is checked
我動態創建的復選框代碼在這里:
$(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);
});
}
});
});
但是,如果選中該復選框,如何在數組中傳遞此復選框值?
在這里你可以這樣做:我使用了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>
使用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.