简体   繁体   English

为JavaScript中的按钮产生动态ID

[英]Producing dynamic id for a button in javascript

I am trying to write a feature where a table data is generated from a database along with buttons like editing that particular row. 我正在尝试编写一个功能,其中从数据库生成表数据以及诸如编辑该特定行之类的按钮。 The data is generated through a foreach from laravel. 数据是通过laravel中的foreach生成的。 One of these buttons is called Edit User . 这些按钮之一称为“ Edit User

列出用户截图

When the Edit User is clicked a form div will be .toggle('show') which will show the form. 单击“ Edit User时,表单div将为.toggle('show') ,将显示该表单。

编辑用户截图

Now I think the problem is the buttons have the same id for the toggle so when I press the second, third and so on buttons the form doesn't toggle. 现在,我认为问题在于按钮的切换ID相同,因此当我按下第二,第三等按钮时,表单不会切换。 Here is my script.js 这是我的script.js

$(document).ready( function() {
  $("#form1").hide();
  $("#form2").hide();
  $("#createuser1").click(function() {
    console.log('Create user button clicked');
    $("#form2").hide();
    $("#form1").toggle('slow');
  });
  $("#edituser1").click(function() {
    console.log('Edit user button clicked');
    $("#form1").hide();
    $("#form2").toggle('slow');
  }); 
});

//start of checkuser
function fetchUser(field, query) {
  console.log('The field is ' + field + ' and the userid is ' + query);
}

my html file (main.blade.php) 我的html文件(main.blade.php)

<tbody>
  @foreach($users as $user)
    <tr>
      <td>{{$user->userid}}</td>
      <td>{{$user->firstname}}</td>
      <td>{{$user->lastname}}</td>
      <td>{{$user->username}}</td>
      <td>{{$user->password}}</td>
      <td>
        @if($user->status == 0)
          Inactive
        @elseif($user->status == 1)
          Active
        @endif
      </td>
      <td>
        <input class="btn btn-danger btn-inverse" type="button" value="Inactive" />
        <input name="edituser" type="button" onclick="fetchUser('edituser', {{$user->userid}})" id="edituser1" class="btn btn-success btn-inverse" value="Edit User"/>
      </td>
    </tr>
  @endforeach
</tbody>

This is the part where it toggles the forms (also part of main.blade.php) 这是切换表单的部分(也是main.blade.php的一部分)

<div class="container" id="form1">
  @include('create')
</div>

<div class="container" id="form2">
  @include('edit')
</div>

I have only included parts of the code to avoid chunks of unrelated code. 我只包括了部分代码,以避免不相关的代码块。 But feel free to ask for any more details. 但请随时询问更多详细信息。

Help me solve the part where the other edit buttons doesn't toggle the edit user form. 帮我解决其他编辑按钮不会切换编辑用户表单的部分。

I think it is better not to have inline click event handler if you have already a click handler in your code. 我认为最好在代码中已经包含单击处理程序的情况下不要使用内联单击事件处理程序。 Change the id to a class: 将ID更改为类:

  <tbody>
                @foreach($users as $user)
                  <tr>
                    <td>{{$user->userid}}</td>
                    <td>{{$user->firstname}}</td>
                    <td>{{$user->lastname}}</td>
                    <td>{{$user->username}}</td>
                    <td>{{$user->password}}</td>

                    <td>
                      @if($user->status == 0)
                        Inactive
                      @elseif($user->status == 1)
                        Active
                      @endif
                    </td>

                    <td>
                      <input class="btn btn-danger btn-inverse" type="button" value="Inactive" />
                      <input name="edituser" type="button" data-id="{{$user->userid}}"  class="btn btn-success btn-inverse editUser" value="Edit User"/>
                    </td>
                  </tr>
                @endforeach
              </tbody>

Then change your js this way: 然后以这种方式更改您的js:

$(document).ready( function() {
  $("#form1").hide();
  $("#form2").hide();
  $("#createuser1").click(function() {
    console.log('Create user button clicked');
    $("#form2").hide();
    $("#form1").toggle('slow');
  });
  $(".editUser").click(function() {
    console.log('Edit user button clicked');
    var id = $(this).attr('data-id');
    fetchUser('edituser', id);
    $("#form1").hide();
    $("#form2").toggle('slow');
  }); 
});

//start of checkuser
function fetchUser(field, query) {
  console.log('The field is ' + field + ' and the userid is ' + query);
}

This way you can reuse the code for all the edituser buttons, you have a much more readible code, you don't have two different click event handler and you don't loose the id of the single user to be passed to the fetchUser function 这样,您可以为所有edituser按钮重用代码,使代码更具可读性,没有两个不同的click事件处理程序,也不会丢失要传递给fetchUser函数的单个用户的ID。

Well, as I see it, you have two options. 好吧,正如我所看到的,您有两种选择。 Unique ids are a must. 唯一ID是必须的。 I'm assuming your userids are unique, so I'd recommend using them as suffixes to create unique ids for your elements. 我假设您的userids是唯一的,所以建议您使用它们作为后缀来为您的元素创建唯一的ID。

First option: Create a unique form for each user that is opened when Edit User is clicked. 第一种选择:为单击“ Edit User时打开的每个用户创建唯一的表单。

Second option: Create a generic form that is populated with the user information when Edit User is clicked. 第二种选择:创建一个单击“ Edit User时填充用户信息的通用表单。

The first is simpler, but the second with be more efficient. 第一个更简单,但是第二个更高效。 Here is example code of the second solution. 这是第二个解决方案的示例代码。 Main HTML: 主要HTML:

<div class="container" id="main">
    <!-- ... -->
    <input type="button" onclick="fetchUser('edituser', {{$user->userid}}, {{$user->firstname}}, {{$user->lastname}})" class="btn btn-success btn-inverse" value="Edit User" />
    <!-- ... -->
</div>

Edit form HTML: 编辑表单HTML:

<div class="container" id="form2">
    <input id='editFormFirstName' />
    <input id='editFormLastName' />
    <input id='editFormPassword' />
</div>

And JS: 和JS:

function fetchUser(field, userid, firstname, lastname) {
    $('#editFormFirstName').val() = firstname;
    $('#editFormLastName').val() = lastname;
    $('#editFormPassword').val() = "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM