简体   繁体   English

如何设置内部存在的选择控件的ID和名称 <td> 使用jQuery的HTML表?

[英]How to set id and name of select control present inside <td> of a HTML table using jQuery?

I've following HTML code for table and select control: 我遵循表格的HTML代码并选择控件:

<table id="blacklistgrid_1"  class="table table-bordered table-hover table-striped">
  <thead>
    <tr id="jumbo">
      <th style="vertical-align:middle">Products</th>
      <th style="vertical-align:middle">Pack Of</th>
      <th style="vertical-align:middle">Quantity</th>
      <th style="vertical-align:middle">Volume</th>
      <th style="vertical-align:middle">Unit</th>
      <th style="vertical-align:middle">Rebate Amount</th>
    </tr>
  </thead>
  <tbody class="apnd-test">
    <tr id="reb1_1">
      <td>
        <div class="btn-group">
          <select name="" id="" class="form-control prod_list">
            <option value='1'>Alabama</option>
            <option value='2'>Alaska</option>
            <option value='3'>Arizona</option>
            <option value='4'>Arkansas</option>
            <option value='5'>California</option>
           </select>
         </div>
       </td>
     </tr>
   </tbody>
 </table>

Now I want to set the name and id of select control from above HTML code to following values: 现在,我想将上述HTML代码中的select控件的名称和ID设置为以下值:

name="product_id_1[1]"
id="product_id_1_1"

How to achieve this? 如何实现呢?

You can use .attr() to set attributes id and name for select.try this 您可以使用.attr()设置select的属性ID和名称。

 $('td select').attr('name','product_id_1[1]');
 $('td select').attr('id','product_id_1_1');

Working Fiddle 工作小提琴

Update: To target first select element: 更新:定位到第一个选择元素:

 $('td select:eq(0)').attr('id','product_id_1_1');

if you don't want to be specific and that is the only select on the page, $('select').attr('name','product_id_1[1]').attr('id','product_id_1_1'); 如果您不想特定,那是页面上的唯一选择,则$('select').attr('name','product_id_1[1]').attr('id','product_id_1_1'); will suffice. 就足够了。

If you need to be more specific: 如果您需要更具体:

$('table#blacklistgrid_1 tbody.apnd-test div.btn-group')
  .attr('name','product_id_1[1]')
  .attr('id','product_id_1_1');

should be good. 应该很好。

the name and id properties can be set using .attr() . nameid属性可以使用.attr()设置。 Other than that it just depends how specific you want to be when finding the select element. 除此之外,它还取决于您在选择select元素时希望有多具体。

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

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