2012-09-05 16 views
0

我有這個問題。我從Tornado獲取數據的矢量。這個矢量是'用戶'。 我有這個塊將js函數調用爲塊for

{% if users %} 
{% for user in users %} 
      <form name="form_user_{{ user.id }}" method="post" action="/networks/{{ net.id }}/rights"> 
       <tr> 
       <td> 
        <div> 
        {{ escape(user.name) }} 
        <input type='hidden' name="id" value='{{ user.id }}'> 
        </div> 
       </td> 
       <td> 

        <label class="radio inline" onclick="document.forms['form_user_{{ user.id }}'].submit();"> 
        <input type="radio" name="optionsRadios" id="optionsRadios1" value="0"> 
        None 
        </label> 
        <label class="radio inline" onclick="document.forms['form_user_{{ user.id }}'].submit();"> 
        <input type="radio" name="optionsRadios" id="optionsRadios2" value="1"> 
        Read 
        </label> 
        <label class="radio inline" onclick="document.forms['form_user_{{ user.id }}'].submit();"> 
        <input type="radio" name="optionsRadios" id="optionsRadios3" value="4"> 
        Read + Commands 
        </label> 



        {{ xsrf_form_html() }} 
       </td> 

       </tr> 
      </form> 
      {% end %} 
      {% end %} 

現在我有一個js函數是這樣的:

$(document).ready(function(){ 
    //function check(){ 

    {% if users %} 
     {% if user.perm == 1 %} 

     $('input:radio[name=optionsRadios]:nth(1)').attr('checked',true); 
     //$('input:radio[name=sex]')[1].checked = true; 

     {% elif user.perm == 4 %} 

     $('input:radio[name=optionsRadios]:nth(2)').attr('checked',true); 
     //$('input:radio[name=sex]')[2].checked = true; 

     {% else %} 

     $('input:radio[name=optionsRadios]:nth(0)').attr('checked',true); 
     //$('input:radio[name=sex]')[0].checked = true; 

     {% end %} 
    {% end %} 

    }); 

但龍捲風告訴我,當用戶的列表是空的變量用戶沒有inizialized。所以我會將函數調用到for塊中,命名函數,例如函數check(){}我不知道... 只有當程序在for塊中輸入時,我如何才能調用函數?

謝謝!

回答

0

爲什麼你不能把這{{user.perm}}隱藏輸入,然後把它從js代碼?

如果你想這樣做,這樣,然後就這樣做:

$(document).ready(function(){ 
    //function check(){ 

    {% for user in users %} 
     {% if user.perm == 1 %} 

     $('input:radio[name=optionsRadios]:nth(1)').attr('checked',true); 
     //$('input:radio[name=sex]')[1].checked = true; 

     {% elif user.perm == 4 %} 

     $('input:radio[name=optionsRadios]:nth(2)').attr('checked',true); 
     //$('input:radio[name=sex]')[2].checked = true; 

     {% else %} 

     $('input:radio[name=optionsRadios]:nth(0)').attr('checked',true); 
     //$('input:radio[name=sex]')[0].checked = true; 

     {% end %} 
    {% end %} 

    }); 
+0

非常感謝您!我剛剛把這個 'code' 'code' 我修改了你的方式,現在js函數結束了!非常感謝你!!! – sharkbait

+0

不客氣) –