我有这样的json格式的数据
{
feild: ...
...
"transport_details": [
{
"allotment_id": ObjectId("5b755710d2ccda0978005d6e"),
"status": "Active"
},
{
"allotment_id": ObjectId("5b755710d2ccda0978005d6e"),
"status": "Inactive"
}
]
}
我写了下面的java脚本函数
function checkIfInActive(transportAllotment, id)
{
if (transportAllotment.length == 0)
{
return '<div class="checkbox checkbox-success">' +
'<input class="commoncheckbox" type="checkbox" id="studentId_-' + id + '' +
'" name="studentId_-' + id + '" value="' + id+ '"' +' >' + '<label></label></div>';
}
else
{
var max = transportAllotment.length-1;
var status = transportAllotment[max]["status"];
if(status != "Active")
{
return '<div class="checkbox checkbox-success">' +
'<input class="commoncheckbox" type="checkbox" id="studentId_-' + id + '' +
'" name="studentId_-' + id + '" value="' + id+ '"' +' >' + '<label></label></div>';
}
else
{
return '<div class="checkbox"><input class="disabled-check" type="checkbox" disabled><label></label></div>';
}
}
}
如果子数组transport_details的最后一个元素的状态为Inactive,我将尝试使用commoncheckbox类返回html。在这种情况下,以上方法不会返回带有class commoncheckbox的复选框。
请帮忙!!!
最佳答案
var myObj = {
"aaa":"zzzz",
"bbb":23,
"transport_details":[
{
"status": "Active"
},
{
"status": "Inactive"
}
]
}
var status = myObj.transport_details[myObj.transport_details.length - 1].status;
var html = (status === "Active")? '<input type="checkbox">' : '<input type="checkbox" disabled>' ;
$('#banner-message').html(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div id="banner-message">
</div>