只有当var不为空时,我才需要在sweetalert html中显示一个链接。这是代码:
$('.patient-details').click(function(e) {
e.preventDefault();
var $this = $(this)
var name = $this.data('name');
var gender = $this.data('gender');
var age = $this.data('age');
var country = $this.data('country');
var address = $this.data('address');
var report = $this.data('report');
swal({
title: name,
html:
"Gender: " + gender +"<br>" +
"Age: " + age +"<br>" +
"Country: " + country +"<br>" +
"Address: " + address +"<br>" +
(report!=undefined?'<a href="' + report + '" target="_blank">View Report</a>':''),
});
});
仅当var报告不为空时,才需要显示报告链接。这是代码笔: https://codepen.io/pamela123/pen/GOJZgo
我试过
if(report){
report = $this.data('report');
}
报告是&#34; 未定义&#34;。 report!= undefined不起作用。
但是如果报告是空的,怎么不在html里面显示报告链接?
我知道这是一个简单的javascript问题,但作为一个新手我无法进一步。
答案 0 :(得分:1)
您可以按照以下更新代码
var htmlTemplate="Gender: " + gender +"<br>" +"Age: " + age +"<br>" + "Country: " + country +"<br>" + "Address: " + address +"<br>" ;
if(report){
htmlTemplate+= '<a href="' + report + '" target="_blank">View Report</a>';
}
swal({
title: name,
html: htmlTemplate,
});
答案 1 :(得分:1)
将数据放在单独的变量中。
然后检查report
是否不是undefined
。如果没有,请将其添加到变量中。
$('.patient-details').click(function(e) {
e.preventDefault();
var $this = $(this)
var name = $this.data('name');
var gender = $this.data('gender');
var age = $this.data('age');
var country = $this.data('country');
var address = $this.data('address');
var report = $this.data('report');
var htmlData = "Gender: " + gender + "<br>" +
"Age: " + age + "<br>" +
"Country: " + country + "<br>" +
"Address: " + address + "<br>";
if( report !== undefined && report != "" ) {
htmlData += '<a href="' + report + '" target="_blank">View Report</a>'
}
swal({
title: name,
html: htmlData
});
});
答案 2 :(得分:0)
$('.patient-details').click(function(e) {
e.preventDefault();
var $this = $(this)
var name = $this.data('name');
var gender = $this.data('gender');
var age = $this.data('age');
var country = $this.data('country');
var address = $this.data('address');
var report = $this.data('report');
swal({
title: name,
html:
"Gender: " + gender +"<br>" +
"Age: " + age +"<br>" +
"Country: " + country +"<br>" +
"Address: " + address +"<br>" +(typeof report !== "undefined" && report != ""?'<a href="' + report + '" target="_blank">View Report</a>':'')
,
});
});