dqwcdqs358367 2013-04-29 10:37
浏览 122

Select2不会创建名称属性

With the select2 jQuery PlugIn I created a tag box. Therefore I used the following html form code:

<form action="someAction" method="post">
    <fieldset>
        <select id="tags" multiple="" name="tags">
            <option value="tag1">val1</option>
            <option value="tag2">val2</option>
            <option value="tag3">val3</option>
        </select>
        <button type="submit" class="btn">Submit</button>
    </fieldset>
</form>

Select2 generates the following field from it:

<input type="text" class="select2-input" autocomplete="off" id="s2id_autogen1" style="width: 10px;">

The problem is here that the name attribute is missing. How can I get the data/text of the input field out of the $_POSTvariable in PHP if there is no name attribute? How can I manage this?

The setup JS is the following:

$(document).ready(function() {
$("#tags").select2({
    placeholder: "Insert tag",
    allowClear: true,
    minimumInputLength: 2,
    maximumSelectionSize: 1,
    minimumWidth: 200
});

});

  • 写回答

5条回答 默认 最新

  • duangang79177 2013-04-29 10:44
    关注

    POST needs name to work, however I notice it has an id:

    id="s2id_autogen1"
    

    Use JS to grab the value by id and post it.

    If the id is ever changing you can use the class:

    HTML:

    <form action="someAction" method="post">
    <fieldset>
        <!-- This will contain the value from the generated input -->
        <input type = 'hidden' name = 'myField' id = 'myField' />
        <select id="tags" multiple="" name="tags">
            <option value="tag1">val1</option>
            <option value="tag2">val2</option>
            <option value="tag3">val3</option>
        </select>
        <button type="submit" class="btn">Submit</button>
    </fieldset>
    

    JS

    $(document).ready(function(){
           $("#tags").select2({
           placeholder: "Insert tag",
           allowClear: true,
           minimumInputLength: 2,
           maximumSelectionSize: 1,
           minimumWidth: 200
    });
    
    //Make it so everytime the input with the class select2-input value is changed
    //the value gets copied into the element with the id myField
    $('input.select2-input').live('change', function(){
        $('input#myField').val($(this).val());
    });
    

    });

    Like this submitting the form will automatically include the value int the POST variable.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀