-1

好的,所以我有一个 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 对象也不允许我将其与自身以外的其他事物进行比较。我确实将它与它本身进行了比较,当然它有效;)。

4

1 回答 1

0

这些是不同的陈述:

if ("5.jpg" == file.name)

if ("5.jpg".localeCompare(file.name))

如果两个字符串相同,则返回第一个比较并运行语句 true体。如果两个字符串相同,则将返回零,这在语句的上下文中与. 你可能想要iflocaleCompareiffalse

{% } else if (output[0].localeCompare(file.name) == 0) { %}
于 2012-08-17T19:04:22.387 回答