Javascript 如何在jquery的.append()中放置if条件
我有一个场景,只有当元素的值不为null时,我才必须附加到td,这是我通过使用I.eJavascript 如何在jquery的.append()中放置if条件,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个场景,只有当元素的值不为null时,我才必须附加到td,这是我通过使用I.e tablerow = $('<tr>'); td = '<td>' + $(this)[0].attrValue+'' if ($(this)[0].countRequired != null) td = td + ' AND ' + $(this)[0].countRequired+'' tablerow=$(''); td='+$(此
tablerow = $('<tr>');
td = '<td>' + $(this)[0].attrValue+''
if ($(this)[0].countRequired != null)
td = td + ' AND ' + $(this)[0].countRequired+''
tablerow=$('');
td='+$(此)[0]。属性值+''
if($(this)[0].countRequired!=null)
td=td+'和'+$(此)[0]。countRequired+''
但是我想通过在.append()中使用if条件来实现
tablerow=$(“”)
.append($('').append($(此)[0].attrValue+'')
if($(this)[0].countRequired!=null){
.append($('').append($(此)[0].countRequired+'')
}
但当我这么做的时候,我发现了一个错误
下面是我的完整代码
函数fetchRUInfo(ruId、functionName、ruOutputId){
$.ajax({
键入:“GET”,
url:“GetRunfo”,
数据:'ruId='+ruId,
成功:功能(数据){
if(functionName==“countByRU”){
$('#showInformationOfRu'+ruId).empty();
var tablebody=$('');
var tablerow=“”;
/*var td=“”*/
$(data.rinfolist)。每个(函数(索引){
/*tablerow=$('');
td='+$(此)[0]。属性值+''
if($(this)[0].countRequired!=null)
td=td+'和'+$(此)[0]。countRequired+''*/
tablerow=$('')
.append($('').append($(此)[0].attrValue+'')
if($(this)[0].countRequired!=null){
.append($(此)[0].countRequired+'')
}
$(tablerow).追加(td);
$(tablebody).append(tablerow);
});
$('')
.attr('id','ruDetails'))
.attr('class','table table striped row单击一个边框-a')
.html('标准')
.append(表体)
.appendTo('div#showInformationOfRu'+ruId);
}else if(functionName==“countByBE”){
$('#showInformationOfRuInBE'+ruOutputId).empty();
var tablebody=$('');
var tablerow=“”;
var td=“”;
$(data.rinfolist)。每个(函数(索引){
tablerow=$('');
td='+$(此)[0]。属性值+''
if($(this)[0].countRequired!=null)
td=td+'和'+$(此)[0]。countRequired+''
$(tablerow).追加(td);
$(tablebody).append(tablerow);
});
$('')
.attr('id','ruDetails'))
.attr('class','table table striped row单击一个边框-a')
.html('标准')
.append(表体)
.appendTo('div#showInformationOfRuInBE'+ruOutputId);
}
},
错误:函数(){
警报('获取资源单元时发生错误,请刷新并重试');
}
});
};
已更新
使用了条件运算符方式,但仍出现错误。。
,因为在表达式的中间插入语句,所以会出错。你不能那样做。在
if
正文中,您将得到如下语句:
.append($('<td>').append($(this)[0].countRequired+''))
或者我们可以使用*:
if($(this)[0].countRequired!=null){tablerow.append($(this)[0].countRequired+'')}它与java有什么关系?@gonczor已删除。。
function fetchRUInfo(ruId, functionName, ruOutputId){
$.ajax({
type: 'GET',
url: "getRUInfo",
data:'ruId='+ruId,
success: function(data) {
if(functionName == 'countByRU'){
$('#showInformationOfRu'+ruId).empty();
var tablebody = $('<tbody>');
var tablerow = "";
/*var td = "";*/
$(data.ruInfoList).each(function(index){
/* tablerow = $('<tr>');
td = '<td>' + $(this)[0].attrValue+''
if ($(this)[0].countRequired != null)
td = td + ' AND ' + $(this)[0].countRequired+''*/
tablerow = $('<tr>')
.append($('<td>').append($(this)[0].attrValue+'')
if ($(this)[0].countRequired != null){
.append($(this)[0].countRequired+''))
}
$(tablerow).append(td);
$(tablebody).append(tablerow);
});
$('<table>')
.attr('id','ruDetails')
.attr('class','table table-striped rowClick one border-a')
.html('<thead><tr><th>Criteria</th></tr></thead>')
.append(tablebody)
.appendTo('div#showInformationOfRu'+ruId);
}else if(functionName == 'countByBE'){
$('#showInformationOfRuInBE'+ruOutputId).empty();
var tablebody = $('<tbody>');
var tablerow = "";
var td = "";
$(data.ruInfoList).each(function(index){
tablerow = $('<tr>');
td = '<td>' + $(this)[0].attrValue+''
if ($(this)[0].countRequired != null)
td = td + ' AND ' + $(this)[0].countRequired+''
$(tablerow).append(td);
$(tablebody).append(tablerow);
});
$('<table>')
.attr('id','ruDetails')
.attr('class','table table-striped rowClick one border-a')
.html('<thead><tr><th>Criteria</th></tr></thead>')
.append(tablebody)
.appendTo('div#showInformationOfRuInBE'+ruOutputId);
}
},
error: function() {
alert('Some error occured while fetching Resource Units, please refresh it and try again');
}
});
};
.append($('<td>').append($(this)[0].countRequired+''))
tablerow.append($('<td>').append(
this.attrValue + '' + (this.countRequired != null ? this.countRequired : '')
));
tablerow.append($('<td>').append(
this.attrValue + '' + (this.countRequired || '')
));
var cellContent = this.attrValue + '';
if (this.countRequired != null) {
cellContent += this.countRequired;
}
tablerow.append($('<td>').append(cellContent));