dqsxsmi3704 2018-04-11 10:26
浏览 60

如何将表单值设置为输入名称但是发送id?

So I have a form input field like that:

<input list="materials" name="material_id" placeholder="Nazwa materiału" autocomplete="off">
    <datalist id="materials">
    @foreach($materials as $material)
    <option value="{{$material->id}}">{{$material->name}}</option>
    @endforeach
    </datalist>

It shows a list of materials (names) but it coresponds to material_id (foreign key) so it has to send an id value but it can be quite confusing for user. How can I set it to send id value but leave "material name" in field? (I know it is probably something from jscript or jquery but I don't know anything about them)

  • 写回答

2条回答 默认 最新

  • duanmiao6695 2018-04-11 10:32
    关注

    you need to use onchange event like this

    $('select').on('change', function() {
      alert( this.value );
      //Do something
    })
    

    your html should be like this

    <select>
        @foreach($materials as $material)
        <option value="{{$material->id}}">{{$material->name}}</option>
        @endforeach
    </select>
    

    example

        <!DOCTYPE html>
        <html>
        <head>
            <title></title>
            <script type="text/javascript">
                $(document).ready(function() {
                    $('select').on('change', function() {
                      alert( this.value );
                    })
                });
            </script>
        </head>
        <body>
        //other things
        <select>
            @foreach($materials as $material)
            <option value="{{$material->id}}">{{$material->name}}</option>
            @endforeach
        </select>
        </body>
        </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大