如何正确更正此找到的具有非唯一 id 错误的元素?

joh*_*hnW 0 php laravel

我有这个表格供用户介绍一些数据以在大会上注册。

首先他需要介绍一些买家信息,然后如果有一些付费票类型还有一些账单信息。然后,如果大会表将“collect_data_from_all_participants”列设为 1,则用户还需要为每个选定的票证类型引入信息。每种门票类型的信息是每个参与者的姓名和姓氏,因此我将“姓名”字段的名称设为“姓名”,将“姓氏”字段的名称设为“姓氏”。吨

然后他的问题出现在控制台中:

 [DOM] Found 4 elements with non-unique id #name: 

`DOM] Found 3 elements with non-unique id #surname: 
Run Code Online (Sandbox Code Playgroud)

您知道纠正问题的最佳方法是什么吗?

<div class="registration_form mt-4">


    <form method="post" class="clearfix">

        <div class="tab-content registration_body bg-white" id="myTabContent">
            <div class="tab-pane fade show active clearfix" id="step1" role="tabpanel" aria-labelledby="home-tab">
                <h6>Buye information</h6>                    

                <div class="form-group font-size-sm">
                    <label for="name" class="text-gray">Name</label>
                    <input type="text" required class="form-control"  id="name" value="{{ (\Auth::check()) ? Auth::user()->name : old('name')}}">
                </div>
                <div class="form-group font-size-sm">
                    <label for="surname" class="text-gray">Surname</label>
                    <input type="text" required class="form-control" name="surname" id="surname" value="{{ (\Auth::check()) ? Auth::user()->surname : old('surname')}}">
                </div>
                <div class="form-group font-size-sm">
                    <label for="email" class="text-gray">Email</label>
                    <input type="text" required class="form-control" name="emai" id="email" value="{{ (\Auth::check()) ? Auth::user()->email : old('email')}}">
                </div>

                @if( array_sum(array_column($selectedRtypes, 'price')) > 0 )

                    <h6>Billing information</h6>

                    <div class="form-group font-size-sm">
                        <label for="name" class="text-gray">Name</label>
                        <input type="text" required class="form-control" id="name">
                    </div>
                    <div class="form-group font-size-sm">
                        <label for="inputName" class="text-gray">Country</label>
                        <input type="text" required class="form-control" id="inputName">
                    </div>
                   <!-- other fields -->

                @endif

                @if (!empty($allParticipants))
                    @if($allParticipants == 1)
                        @foreach($selectedRtypes as $k=>$selectedRtype)
                            <h6 class="text-heading-blue mb-3 pb-2 font-weight-bold">Participant - 1 - {{$k}}</h6>

                            <div class="form-group font-size-sm">
                                <label for="name" class="text-gray">Name</label>
                                <input type="text" required class="form-control" id="name" value="">
                            </div>
                            <div class="form-group font-size-sm">
                                <label for="surname" class="text-gray">Surname</label>
                                <input type="text" required class="form-control" name="surname" id="surname" value="">
                            </div>
                        @endforeach
                    @endif
                @endif

                <button type="button" href="#step2" data-toggle="tab" role="tab"
                        class="btn btn-primary btn float-right next-step">
                    Go to step 2
                </button>
            </div>
            <div class="tab-pane fade clearfix" id="step2" role="tabpanel" aria-labelledby="profile-tab">
                <form method="post" class="clearfix">

                    <h6>Select payment type</h6>

                   <!-- step 2 fields-->

                    <div class="text-right">
                    <button type="button" href="#step3" data-toggle="tab" role="tab"
                            class="btn btn-outline-primary prev-step">
                        Go to step 2
                    </button>
                    <button type="button" href="#step3" data-toggle="tab" role="tab"
                            class="btn btn-primary btn ml-2 next-step">
                        Go to step 3
                    </button>
                    </div>
                </form>
            </div>
            <div class="tab-pane clearfix fade" id="step3" role="tabpanel" aria-labelledby="contact-tab">
                <form method="post" class="clearfix">
                    <!-- step 3 fields-->
                </form>
            </div>
        </div>
    </form>
</div>
Run Code Online (Sandbox Code Playgroud)

Dan*_*iel 5

您的问题是单个文档的所有 id 属性必须是唯一的。来自MDN

id 全局属性定义了一个在整个文档中必须是唯一的唯一标识符 (ID)。它的目的是在链接(使用片段标识符)、脚本或样式(使用 CSS)时识别元素。

您遍历<input>id为元素name,并surname导致具有相同ID的多个元素。如果您从这些输入元素中删除 id,您的警告就会消失。