2009-08-25 34 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>