Stackoverflow 上的第一篇文章,请多多包涵。我有一个 JQMobile Web 应用程序,它通过 JSON 生成动态音频文件链接,并为每个 id 附加音频播放器。当找不到 src 时,我试图隐藏播放器。
php/mysql/jqm 脚本正在运行,我似乎无法获得 if/else 部分,它总是显示播放器是否为空。
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<script type="text/javascript">
var serviceURL = "http://localhost/Apps/services/";
$(document).on('pageshow', '#detailsPage', function(event) {
var id = getUrlVars()["id"];
$.getJSON(serviceURL + 'getartist.php?id='+id, displayArtist);
});
function displayArtist(data) {
var artistDetVal = data.item;
console.log(artistDetVal);
$('#artistDetPic').attr('src', 'pics/lg/' + artistDetVal.picture);
$('#fullName').text(artistDetVal.firstName + ' ' + artistDetVal.lastName);
$('#artistDetTitle').text(artistDetVal.bio);
$('#city').text(artistDetVal.city);
$('#audioBio').attr('src', 'audio/' + artistDetVal.audio) .detach().appendTo($("#audioPlayer"));
}
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
</script>
<body>
<script type="text/javascript">
$(document).ready(function(){
function displayArtist(data) {
var artistDetVal = data.item;
$('#audioBio').text(artistDetVal.audio);
if (artistDetVal.audio>0) {
$("#audioPlayer").show();
}
else {
$("#audioPlayer").hide();
}
}
});
</script>
<audio id="audioPlayer" controls="controls"><source id="audioBio" src=""/></audio>
</body>
</html>