-1

わかりましたので、パラメーターとして test() 関数に送信する JSON オブジェクトがあります。下記参照:

<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))

2 つの文字列が同じ場合、最初の比較が返され、ステートメント trueの本体が実行されます。2 つの文字列が同じである場合、 はゼロを返します。これは、ステートメントのコンテキストではと同じです。あなたはおそらくしたいですiflocaleCompareiffalse

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