2011-10-12 80 views
0

检索和循环当我点击按钮,我产生了一些输入自定义字段属性那样:输入自定义属性值:在PHP

<input type='text' name='field["+ i++ +"]' value='' data-kind='title' /> 
<input type='text' name='field["+ i++ +"]' value='' data-kind='video' /> 
<input type='text' name='field["+ i++ +"]' value='' data-kind='text' /> 

我检索与PHP中的foreach循环的“名”值:

$result = array_combine($num, $records); 

    foreach ($result as $rank => $content) 
    { 
     $data = array(
      'content' => $content, 
      'post_id' => $post_id, 
      'rank' => $rank, 
      'type' => $this->input->post('field_type') // HERE 
      ); 
       echo '<pre>';print_r($data);echo '</pre>'; 
    } 

为了得到 '型' 我做的这是由该给出$this->input->post('field_type');

var field_type = $(":input[data-kind]").attr('data-kind'); 
$("#field_type").val(field_type' '); 

和:

echo '<input type="hidden" id="field_type" name="field_type" value="" />'; 

但它只返回我的最后一个“数据实物的价值不是每个人:/

现在我只需要循环为每个输入字段”数据实物的价值在我的foreach循环检索它们

任何帮助将是非常非常感谢!


非常感谢您的回答,它帮了我很多!但现在我怎么能在“类型”数据添加的结果在我目前的foreach:

$result = array_combine($num, $records); 

    foreach ($result as $rank => $content) 
    { 
     $data = array(
      'content' => $content, 
      'post_id' => $post_id, 
      'rank' => $rank, 
      'type' => // HERE I NEED EACH ATTRIBUTE VALUE 
      ); 
       echo '<pre>';print_r($data);echo '</pre>'; 
    } 

回答

1

如果你想将所有的data-kind值在#field_type领域,你需要的东西是这样的:

var fieldTypes = []; 
$("input[data-kind]").each(function() 
{ 
    fieldTypes.push($(this).attr('data-kind')); 
}); 
$("#field_type").val(fieldTypes.join(',')); 
0

也许你已经错过了加号? $("#field_type").val(field_type' '); 应该$("#field_type").val(field_type+' ');

0

此代码: http://jsfiddle.net/PKgkU/17/

你想要做什么!

$('input').each(function(el) { 
    switch ($(this).data('kind')) { 
    case "video": 
     kind = 'video'; 
     break; 
    case "image": 
     kind = 'image'; 
     break; 
    case "title": 
     kind = 'title'; 
     break; 
    default: 
     break; 
    } 
    $(this).after('<input type="hidden" id="field_type" name="field_type" value="' + kind + '" />'); 
});