简体   繁体   English

输入自定义属性值:在PHP中检索和循环

[英]Input custom attribute value : retrieve and loop in PHP

When I click on buttons I generate some inputs field with custom attributes like that : 当我单击按钮时,会生成一些带有自定义属性的输入字段,例如:

<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' />

I retrieve the 'name' value with a foreach loop in PHP : 我在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>';
    }

To get the 'type' I do a $this->input->post('field_type'); 要获取“类型”,我需要执行$this->input->post('field_type'); which is given by this : 这是给定的:

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

and : 和:

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

But it only returns me the last 'data-kind' value not each one :/ 但这只会返回我最后一个“数据类型”值,而不是每个值:/

Now i just need to loop the 'data-kind' value for each input fields and retrieve them in my foreach loop 现在,我只需要循环每个输入字段的“数据类型”值并在我的foreach循环中检索它们

Any help would be very very appreciated !! 任何帮助将非常非常感谢!


Many thanks for your answers, it helped me a lot! 非常感谢您的回答,对我有很大帮助! But now how can I add the result in my current foreach at 'type' data : 但是现在我如何在“类型”数据中将结果添加到当前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>';
    }

If you want to place all of the data-kind values in the #field_type field, you need something like this: 如果要将所有data-kind值都放在#field_type字段中,则需要这样的内容:

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

Maybe you've missed the plus sign? 也许您错过了加号? $("#field_type").val(field_type' '); should be $("#field_type").val(field_type+' '); 应该是$("#field_type").val(field_type+' ');

This code: http://jsfiddle.net/PKgkU/17/ 这段代码: http : //jsfiddle.net/PKgkU/17/

Does what you want! 你想要什么!

$('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 + '" />');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM