【问题标题】:how to select a form inside a datatables without an id如何在没有 id 的数据表中选择表单
【发布时间】:2020-07-17 14:00:54
【问题描述】:

我用 html laravel 集合将这个表单写在一个表格中:

 @foreach($patients as $patient)
            <tr>
                {{Form::open(['route' => 'patientDetails'])}}
                {{Form::hidden('patient',json_encode($patient))}}
                {{Form::close()}}
                <td>{{$patient['display']}}</td>
                .......
                </td>
                </a>
            </tr>           
        @endforeach

        </tbody>
        <tfoot>

然后我编写了这个脚本部分,当用户点击表格的一行时,它可以让用户进入一个新页面,数据以隐藏形式呈现。

$(document).ready(function(e) {
    $('#patients').DataTable({
        responsive: true,
        fixedColumns:   true,
        columnDefs: [
            {
                targets: "_all",
                className: 'dt-body-center'
            }
        ]
    });
    var table = $('#patients').DataTable();
    $('#patients tbody').on('click', 'tr', function () {
        var data = table.row( this ).data();
        if (window.confirm( 'Stai per accedere ai dettagli di: '+data[0]+'' )) {
         //here I need a way to select the form inside the row just cliked
            console.log(data); //this do not contains the hidden form
        };});});

也许我混合了太多概念,并且有一种简单的方法可以实现相同的目标(单击此表的一行并通过 post 方法转到带有一些附加数据的下一页)但实际上我想找到一种方法通过 jquery 选择行内的表单,然后进行提交。

【问题讨论】:

  • 所以,如果我是对的,请告诉我:您想要的是在用户单击包含它的行时提交表单?另外:新页面是什么意思?新标签还是同一个标签?
  • @Scaramouche 你是对的,在点击 js "window.confirm" 上的确认按钮后,用户应该在同一个选项卡中进入一个新页面

标签: jquery laravel forms datatables laravelcollective


【解决方案1】:

使用类,以便您更轻松地使用 jquery 选择器

<tbody>
    @foreach ($patients as $patient)
    <tr>
        <td>{{ $patient['display'] }}</td>
        <td>
            <form class="table-form" action="..." method="...">
                <input class="table-hidden" type="hidden" value='@json($patient)' name="patient">
                <button type="submit">View</button>
            </form>
        </td>
    </tr>
    @endforeach
</tbody>
$(() => {
    let table = $('#patients').DataTable({
        responsive: true,
        fixedColumns: true,
        columnDefs: [
            {
                targets: "_all",
                className: 'dt-body-center'
            }
        ]
    });

    $('.table-form').on('submit', e => {
        let patient = JSON.parse($(this).find('.table-hidden').val());

        if(window.confirm(`Stai per accedere ai dettagli di: ${patient[0]}`) {
            // console.log(patient);
        } else {
            e.preventDefault();
        }
    });
});

【讨论】:

  • 这里有一个明确的按钮来点击并执行提交,这不是我的目标,我喜欢点击行(就像我写的函数的第一部分一样),然后在确认之后让用户到另一个页面...但我认为我可以使用您的逻辑添加一些有用的类,谢谢
  • 我尝试在表单上添加一个唯一 ID,并使用 'var form = document.getElementsByTagName(riga);' 来捕获它但表格似乎不是“可提交的”
【解决方案2】:

因此,在此示例中,有两行,每行在其第一个 TD 中都有一个表单。您所要做的就是将处理程序附加到每个 TR 并使用 jQuery 的选择器在该 TR 中查找表单。最后你只需调用submit就可以了。

我希望这个想法对你有所帮助。

$(document).ready(function(e) {
    const table = $('#patients').DataTable({
        responsive: true,
        fixedColumns:   true,
        columnDefs: [
            {
                targets: "_all",
                className: 'dt-body-center'
            }
        ]
    });
    
    table.on('click', 'tr', function () {
        const form = $(this).find('form').first();
        if (window.confirm( 'Stai per accedere ai dettagli di: '+form.attr('action')+'' )){
         //here I need a way to select the form inside the row just cliked
          //  console.log(data); //this do not contains the hidden form
        
          form.submit()
        };
    });
        
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" rel="stylesheet"/>

<table id="patients" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Form</th>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
        <tr>
                <td>
                <form method='get' action='https://google.com'>
                  
                  <input type='submit' value='GOOGLE'/>
                </form>
                </td>

                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
            <td>
                <form method='get' action='https://wikipedia.com'>
                  <input type='submit' value='WIKIPEDIA'/>
                </form>
                </td>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
          </tbody>
           </table>

【讨论】:

  • 我只使用“常量”部分,它就可以了!非常感谢
猜你喜欢
  • 1970-01-01
  • 2021-03-03
  • 2021-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 2017-10-04
相关资源
最近更新 更多