好的,我有一个JSON对象,我作为参数发送到test()函数。见下文:
<script type="text/javascript">
$('#getdata').click(function(){
$.ajax({
url: '<?php echo base_url().'jqueryDB/jquery';?>', //the script to call to get data
type:'POST',
data: "", //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(output_string){
$("#result_table").append(output_string);
test(output_string);
}
});
});
</script>
我的测试功能如下所示:(评论部分解释更多)
<script id="template-download" type="text/x-tmpl">
window.output = []; // global array
function test(arg){
output = arg;
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
{% if (file.error) { %}
<td></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
// This is where I am having problems. I specify index zero for output and sooner or
// later file.name should match, but it never does and I'm not sure why??
// So then I do else if ( "5.jpg" == file.name ) and it does work....As you can
// see that I test it in the line right below the else if, and it displays the same as file.name.
{% } else if ( output[0].localeCompare( file.name ) ) { %}
<td class="name">{%=String(output[0])%}{%=String(file.name)%}</td>
<td class="preview">{% if (file.thumbnail_url) { %}
<a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
{% } %}</td>
<td class="name">
<a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
</td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td colspan="2"></td>
{% } %}
<td class="delete">
<button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
<i class="icon-trash icon-white"></i>
<span>{%=locale.fileupload.destroy%}</span>
</button>
<input type="checkbox" name="delete" value="1">
</td>
</tr>
}
{% } %}
}
在我的比较中,谁能看到我出错的地方?当我else if ( output[0].localeCompare( "5.jpg" ) ) { %}
时,它不起作用。因此,即使我测试它时屏幕上的输出显示5.jpg,JSON对象也不允许我将它与其他东西进行比较。我确实将它与自身进行了比较,当然它有效;)。
答案 0 :(得分:0)
这些是不同的陈述:
if ("5.jpg" == file.name)
if ("5.jpg".localeCompare(file.name))
如果两个字符串相同,则第一个比较将返回true
,并且将运行if
语句的正文。如果两个字符串相同,则localeCompare
将返回零,在if
语句的上下文中,{{3}}与false
相同。你可能想要
{% } else if (output[0].localeCompare(file.name) == 0) { %}