简体   繁体   English

表中的输入和两个按钮并排

[英]Input and two buttons side by side in table

I got an input and two icons (that work like buttons) and I want them to be side by side in a table. 我有一个输入和两个图标(类似于按钮),我希望它们在表中并排放置。 Besides, when on click of the icons ("buttons"), I want it to get me the value of the input. 此外,当单击图标(“按钮”)时,我希望它为我获取输入值。

 $(document).on("click", "#add", function() { alert($("#add").closest()); }); 
 .w3-table { border-collapse: collapse; border-spacing: 0; width: 100%; display: table; } #quantidade input { width: 40%; height: 40px; } #quantidade div { font-size: 15pt; display: grid; padding-left: 10px; } #quantidade i { padding: 0 3px; } #quantidade i:hover { background-color: #0F292F; cursor: pointer; } #quantidade { padding-left: 80px; } table { width: 40%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <table class="w3-table"> <thead> <tr> <th colspan="3"> <h2>Lista das Compras</h2> </th> </tr> </thead> <tbody> <tr> <td>Arroz</td> <td id='quantidade' class='w3-right'> <input type='text' class='w3-input w3-left' value='1'> <div> <i id='add' class='fa fa-angle-up'></i> <i id='rem' class='fa fa-angle-down'></i> </div> </td> </tr> </tbody> </table> 

Demo 演示版

  <tr>
    <td>Arroz</td>
    <td id='quantidade'>
      <input type='text' id='button1' value='1'>
    </td>
    <td>
      <div>
        <span id='add' class='fa fa-angle-up'></span><br>
        <span id='rem' class='fa fa-angle-down'></span>
      </div>
    </td>
  </tr>

$(document).on("click", "#add", function(){
    alert($("#button1").val());
});

Try the following: 请尝试以下操作:

 $(document).on("click", "#add", function(){ alert($("#add").closest('td').prev().find('input[type=text]').val()); }); 
 .w3-table { border-collapse:collapse; border-spacing:0; width:100%; display:table; } #quantidade input { width: 40%; height: 40px; } #quantidade div { font-size: 15pt; display: grid; padding-left: 10px; } #quantidade i {padding: 0 3px;} #quantidade i:hover { background-color: #0F292F; cursor:pointer; } #quantidade {padding-left: 80px;} table {width: 40%;} 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="w3-table"> <thead> <tr> <th colspan="3"><h2>Lista das Compras</h2></th> </tr> </thead> <tbody> <tr> <td>Arroz</td> <td> <input type='text' id='button1' value='1'> </td> <td> <div> <span id='add' class='fa fa-angle-up'></span><br> <span id='rem' class='fa fa-angle-down'></span> </div> </td> </tr> </tbody> </table> 

Change your jquery code 更改您的jQuery代码

 $(document).on("click", "#add", function () {
        alert($(this).parent().closest('div').prev('input').val());
    });

As you said you have changed your table structure like this 就像你说的那样,你已经改变了表的结构

<table class="w3-table">
            <thead>
              <tr>
                <th colspan="3"><h2>Lista das Compras</h2></th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>Arroz</td>
                <td>
                  <input type='text' id='button1' value='1'>
                </td>
                <td>
                  <div>
                    <span id='add' class='fa fa-angle-up'></span><br>
                    <span id='rem' class='fa fa-angle-down'></span>
                   </div>
                </td>
                </tr>
                </tbody>
                </table>

So code for new structure table will be 所以新结构表的代码将是

$(document).on("click", "#add", function () {
            alert($(this).parent().closest('div').parent().prev('td').find('#button1').val());
        });

Hope this will help you. 希望这会帮助你。

It works with this: 它适用于此:

$(document).on("click", "#add", function(){
    alert($(this).closest("tr").find('input').val());
});

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

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