【问题标题】:Input custom attribute value : retrieve and loop in PHP输入自定义属性值:在 PHP 中检索和循环
【发布时间】:2011-12-06 12:06:45
【问题描述】:

当我点击按钮时,我会生成一些带有自定义属性的输入字段:

<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-&gt;input-&gt;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>';
    }

【问题讨论】:

    标签: php jquery codeigniter input foreach


    【解决方案1】:

    如果您想将所有 data-kind 值放在 #field_type 字段中,您需要这样的内容:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        这段代码: 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 + '" />');
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-24
          • 1970-01-01
          • 2018-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-23
          相关资源
          最近更新 更多