2009-08-25 31 views
0
循環問題

我使用JQuery我在哪裏嘗試使用CakePHP的控制器在JQuery的input元素的返回值..對於JQuery的

我從我的CakePHP控制器操作返回兩個變量 $項目和$屬性.. $屬性將返回字段名及其類型,大小爲 $條目將返回字段名和爲該字段提交的值。

兩者都是數組變量 在這裏,我已經創建使用

   <?php foreach ($attributes as $r): ?> 
       $("<div id= <?php echo $r['Attribute']['label'];?> ></div>").appendTo("#main"); 
       $("<input id=input<?php echo $r['Attribute']['id'];?> type= 'text' style= 'width:<?php echo $r['Attribute']['size'];?>px'value='<?php echo $attribute['Result']['value'];?>' ?> ></input><br>").appendTo("#main"); 
      $("<div id= <?php echo $r['Attribute']['type'];?> ></div>").appendTo("#main"); 
       <?php endforeach; ?> 

在上面的代碼中,我創造它顯示我基於它。但是內正確輸入元素的輸入元素對應的輸入元素當我試圖使用像 值= '' 輸入元件?>

我必須保持

   <?php endforeach;?> 

其中這裏面只有我能使用

怎麼做。請給我建議.. 因爲兩者都是循環我不知道如何使用它們,因爲當我保持 將作爲創建多次數..

<script type="text/javascript"> 
     $(document).ready(function(){ 
      $(".edi").click(function(){ 


       <?php foreach ($attributes as $r): ?> 
       $("<div id= <?php echo $r['Attribute']['label'];?> ></div>").appendTo("#main"); 
       $("<input id=input<?php echo $r['Attribute']['id'];?> type= 'text' style= 'width:<?php echo $r['Attribute']['size'];?>px'value='<?php echo $attribute['Result']['value'];?>' ?> ></input><br>").appendTo("#main"); 
      $("<div id= <?php echo $r['Attribute']['type'];?> ></div>").appendTo("#main"); 
       <?php endforeach; ?> 
       $(".edi").hide();$(".vie").show(); 
       return false; 
      }); 
     }); 
     </script> 

編輯: 我一直 爲retriving從屬性表中的字段(類型,大小,字段名).. 是retriving的,表格中的條目(標籤,值)...

在點擊編輯按鈕,,,我生成與尺寸的輸入元件,其中i從$ R [「屬性」] [「尺寸」]得到它作爲

$(」類型=‘文本’ style ='width:px'value =''?>>
「).appendTo(」#main「); 這顯示了正確的輸入元素的正確大小,它從表中檢索到的正確大小。

現在在這個ie中,在Input元素中,我想顯示這個對應的字段的值, $ R1 [ '結果'] [ '值']; 這是我不能夠讓這個值顯示裏面輸入Elements..Please幫我......

+0

請寫出的HTML/Javascript代碼結果foreach – 2009-08-25 13:15:22

+1

考慮編輯你的文章的後半部分。這句話是不完整的。我不太明白爲什麼你有php和js代碼混合。最終的結果是什麼? – krishna 2009-08-25 13:17:49

+0

編輯帖子 – useranon 2009-08-27 10:27:15

回答

1

不知道如果我理解你的問題,但這裏是我的回答:

<script type="text/javascript"> 
    $(document).ready(function() { 
    $(".edi").click(function() { 
     <?php 
     // loop over attributes 
     foreach ($attributes['Attribute'] as $attribute): 
      // loop over results 
      foreach ($entries['Result'] as $result): 
      // determine attribute value 
      if ($result['fieldname'] == $attribute['fieldname']): 
       $attribute['value'] = $result['value']; 
      endif; 
      endforeach; 
      // build html string 
      $html = String::insert(
      '<div id=":label"></div><input id="input:id" type="text" style="width: :size px" value=":value"></input><br><div id=":type"></div>', 
      $attribute // see previous version for expanded version of this line 
     ); 
      // append it using jquery 
      echo "$('" . $html . "').appendTo('#main');"; 
     endforeach; 
     ?> 
     $(".edi").hide(); 
     $(".vie").show(); 
     return false; 
    }); 
    }); 
</script> 
0

的問題不是很清楚,但這裏是一個建議

爲什麼不創建一個新的視圖文件來渲染要附加到#main的HTML?
您可以從控制器動作的屬性數組傳遞給視圖文件
例如,認爲可能是「ViewToAppend.ctp」:

<?php foreach ($attributes as $r): ?> 
    <div id="<?php echo $r['Attribute']['label'];?>" /></div> 
    <input id="input<?php echo $r['Attribute']['id'];?>" type="text" style= "width:<?php echo $r['Attribute']['size'];?>px" value="<?php echo $attribute['Result']['value'];?>"> </input><br> 
    <div id="<?php echo $r['Attribute']['type'];?>"></div> 
<?php endforeach; ?> 

說你的控制器「YourController」,和你的行動稱爲「YourAction」。
在填充$ attributes數組並將其設置爲視圖變量的初始代碼之後,檢查請求是否爲ajax。
如果它是一個Ajax請求,只渲染你的小景 「ViewToAppend.ctp」

class YourController extends AppController{ 
    var $components = array(... , 'RequestHandler', ...); 
    ... 
    function YourAction(/* parameters */){ 

     /* initial code where you set up $attributes */ 

     $this->set('attributes',$attributes); 

     if($this->params['isAjax']) 
      render('/path/to/view/ViewToAppend','ajax'); 
    } 
    ... 
} 

,那麼你可以稱之爲 「YourAction」 從JavaScript

<script type="text/javascript"> 
$(document).ready(function(){ 
    $(".edi").click(function(){ 

     $.ajax({ 
      url:'YourController/YourAction/...', 
      success:function(msg){ 
       $('#main').append(msg); 
       $(".edi").hide(); 
       $(".vie").show(); 
      } 
     }); 

     return false; 
    }); 
}); 
</script>