如何识别<select>中的特定项目,并在jquery中的每一行添加删除按钮

时间:2016-11-07 09:13:16

标签: javascript jquery html

1.)我创建了一个嵌套表,然后我想点击“删除”#39;子表内的按钮,其行将被删除。 2.)我有&lt; select&gt;标签。问题是我如何进行验证,检查一个客户是否已经选择了项目类型,但是也可以在下一个客户处选择此选定项目。 例如,Customer1选择Iron。现在,Customer1无法在下一个项目中选择Iron类型的项目。 Customer1也可以选择铜或木材,但不能选择铁。然后,如果我有Customer2,则可以选择Iron类型的Item。 这是我的代码: &#13; &#13;     $(&#34; .addItem&#34;)。on(&#39;点击&#39;,功能(e){&#13;           $(this).closest(&#39; table&#39;)。find(&#34; .item:last&#34;)。after(&#39;&lt; tr class =&#34; item&#34 ;&GT;&LT; TD&GT;&LT;按钮&GT;删除&LT; /按钮&GT;&LT; / TD&GT;&LT; TD&gt;新建项目&lt; / TD&GT;&LT; TD&GT;&LT; / TD&GT;&LT; TD&GT;&LT; / TD&GT;&LT ; td&gt;&lt; / td&gt;&lt; td&gt;&lt; select name =&#34;&#34; id =&#34;&#34;&gt;&lt; option value =&#34;&#34; &gt; Iron&lt; / option&gt;&lt; option value =&#34;&#34;&gt; Copper&lt; / option&gt;&lt; option value =&#34;&#34;&gt; Wood&lt; / option&gt;&lt; /选择&GT;&LT; / TD&GT;&LT; / TR&GT;&#39;);&#13;         });&#13;     &#13;     &#13;         $(&#34; .addCustomer&#34;)。on(&#39;点击&#39;,功能(e){&#13;           var newCustomer = $(&#39; .customer:last&#39;)。after(&#39;&lt; tr class =&#34; customer&#34;&gt;&lt; td&gt; New Customer&lt; / td&gt;&lt; ; TD&GT;&LT; / TD&GT;&LT; TD&GT;&LT; / TD&GT;&LT; TD&GT;&LT; / TD&GT;&LT; / TR&GT;&#39;);&#13;           var newCart = $(&#39; .cart:first&#39;)。clone(true,true).wrap(&#39; td&#39;);&#13;           newCart.find(&#39; tbody&#39;)。html(&#34;&#34;)。html(&#39;&lt; tr class =&#34; item&#34;&gt;&lt; td&gt; ;&LT;按钮&GT;删除&LT; /按钮&GT;&LT; / TD&GT;&LT; TD&gt;新建项目&lt; / TD&GT;&LT; TD&GT;&LT; / TD&GT;&LT; TD&GT;&LT; / TD&GT;&LT; TD&GT;&LT; / td&gt;&lt; td&gt;&lt; select name =&#34;&#34; id =&#34;&#34;&gt;&lt; option value =&#34;&#34;&gt; Iron&lt; / option&gt ;&lt;选项值=&#34;&#34;&gt;铜&lt; /选项&gt;&lt;选项值=&#34;&#34;&gt; Wood&lt; / option&gt;&lt; / select&gt;&lt; / td&gt; ;&LT; / TR&GT;&#39;);&#13;           $(&#39;。客户:上&#39;)。追加(newCart);&#13;           e.preventDefault();&#13;         });&#13;     &#13;     &#13;         $(&#39; .itemType&#39;)。on(&#39; change&#39;,function(){&#13;                 &#13;             var selected = $(this)find(&#39; option:selected&#39;)。val();&#13;     &#13;             if($(this).find(&#39; option:selected&#39;)。val()=== selected){&#13;                 警告(&#34;项目已被选中。&#34;);&#13;             }&#13;             &#13;         });&#13;  表,&#13;         TD,&#13;         th {&#13;           边框:1px纯黑色;&#13;         }&#13; &lt; script src =&#34; https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"&gt;&lt; / script&gt;&#13;       &lt; meta name =&#34; viewport&#34; content =&#34; width = device-width,initial-scale = 1,user-scalable = no&#34;&gt;&#13;       &LT;标题&GT; PO&LT; /标题&GT;&#13; &#13;       &LT;报头GT;&#13;         &lt; form id =&#34; panel&#34;&gt;&#13;     &#13;         &LT; /形式&GT;&#13;       &LT; /报头&GT;&#13;       &lt; section id =&#34; I&#34;&gt;&#13;     &#13;     &#13;         &lt; table id =&#39; PO1&#39;类=&#34; purchaseOrder的&#34;&GT;&#13;           &LT; THEAD&GT;&#13;             &LT; TR&GT;&#13;               &LT的第i;姓名&LT; /第&GT;&#13;               &LT的第i;年龄&LT; /第&GT;&#13;               &LT;第&gt;地址&LT; /第&GT;&#13;               &LT的第i;性别和LT; /第&GT;&#13;               &LT的第i;购物车和LT; /第&GT;&#13;             &LT; / TR&GT;&#13;           &LT; / THEAD&GT;&#13;           &LT; TBODY&GT;&#13;             &lt; tr id =&#39; C1&#39;类=&#39;顾客&#39;&GT;&#13;               &lt; td&gt;一些名称&lt; / td&gt;&#13;               &LT; TD&GT; 30℃; / TD&GT;&#13;               &lt; td&gt;我的地址&lt; / td&gt;&#13;               &LT; TD&GT;男性&LT; / TD&GT;&#13;               &LT; TD&GT;&#13;                 &lt; table class =&#39; cart&#39;&gt;&#13;                   &LT; THEAD&GT;&#13;                     &LT; TR&GT;&#13;                       &LT的第i;删除&LT; /第&GT;&#13;                       &LT的第i;品牌&LT; /第&GT;&#13;                       &LT的第i;数量&LT; /第&GT;&#13;                       &LT的第i;尺寸&LT; /第&GT;&#13;                       &LT的第i;颜色&LT; /第&GT;&#13;                       &LT的第i;的类型&lt; /&第GT;&#13;                     &LT; / TR&GT;&#13;                   &LT; / THEAD&GT;&#13;                   &LT; TBODY&GT;&#13;                     &lt; tr id =&#39; item1&#39;类=&#39;项目&#39;&GT;&#13;                       &LT; TD&GT;&LT;按钮&GT;删除&LT; /按钮&GT;&LT; / TD&GT;&#13;                       &LT; TD&GT;雅马哈&LT; / TD&GT;&#13;                       &LT; TD&GT; 30℃; / TD&GT;&#13;                       &LT; TD&gt;轻便&LT; / TD&GT;&#13;                       &LT; TD&GT;黑色&LT; / TD&GT;&#13;                       &LT; TD&GT;&#13;                       &lt; select name =&#34;&#34;类=&#34; ITEMTYPE&#34;&GT;&#13;                       &lt; option value =&#34; i&#34;&gt; Iron&lt; / option&gt;&#13;                       &lt; option value =&#34; c&#34;&gt; Copper&lt; / option&gt;&#13;                       &lt; option value =&#34; w&#34;&gt; Wood&lt; / option&gt;&#13;                       &LT; /选择&GT;&#13;                       &LT; / TD&GT;&#13;                     &LT; / TR&GT;&#13;                   &LT; / tbody的&GT;&#13;                   &LT; TFOOT&GT;&#13;                     &LT; TR&GT;&#13;                       &lt; td colspan =&#39; 5&#39;&gt;&lt; / td&gt;&#13;                       &LT; TD&GT;&#13;                         &lt; button class =&#34; addItem&#34;&gt; Add Item&lt; / button&gt;&#13;                       &LT; / TD&GT;&#13;                     &LT; / TR&GT;&#13;                   &LT; / TFOOT&GT;&#13;                 &LT; /表&gt;&#13;     &#13;               &LT; / TD&GT;&#13;             &LT; / TR&GT;&#13;           &LT; / tbody的&GT;&#13;           &LT; TFOOT&GT;&#13;             &LT; TR&GT;&#13;               &lt; td colspan =&#39; 3&#39;&gt;&lt; / td&gt;&#13;               &LT; TD&GT;&#13;                 &lt; button class =&#34; addCustomer&#34;&gt;添加客户&lt; / button&gt;&#13;               &LT; / TD&GT;&#13;             &LT; / TR&GT;&#13;           &LT; / TFOOT&GT;&#13;         &LT; /表&gt;&#13;     &#13;     &#13;       &LT; /节&gt;&#13;       &LT;页脚&GT;&安培; NBSP;&LT; /页脚&GT;&#13; &#13; &#13;

5 个答案:

答案 0 :(得分:0)

要点击任何“删除”按钮删除相应的行,您可以尝试使用此

  $(document).on('click', '.item button', function(){
    $(this).parent().parent().remove();
  });

Here is a Codepen

我注意到的另一件事是,你想在$('.itemType').on('change', function()做什么?因为到目前为止,您所做的只是检查所选选项到ITSELF的值,这显然是真的。

答案 1 :(得分:0)

您必须传递一个元素ID,该ID将从客户的购物车和选定的行中删除,如下所示;

<tr class="item"><td><button data-id="1">Delete</button></td><td>New item</td><td></td><td></td><td></td><td><select name="" id=""><option value="">Iron</option><option value="">Copper</option><option value="">Wood</option></select></td>

jquery part:

    $("tr.item button").click(function(){
  var item_to_remove = $(this).attr("data-id");
  var row = $(this).parent().parent();



$.ajax({
    url: "url to cart handler for delete item",
    data: "id="+item_to_remove,
    type: "post",
    success: function(data){
        row.remove();
    },
    error: function(){
        console.log("ajax req for delete failed");
    }
  });
});

答案 2 :(得分:0)

我修复了Delete问题。在验证方面,回答我的下面的问题,我将修复相同的问题。

好的,我现在正确处理了一个空项目并禁用了其他项目上的选定值。

&#13;
&#13;
$(".addItem").on('click', function(e) {
      var parent = $(this).closest('table');
      var lastItem = parent.find(".item:last");
      var newItem = $('<tr class="item"><td><button>Delete</button></td><td>New item</td><td></td><td></td><td></td><td><select name="" id="" class="itemType"><option value="-1"></option><option value="i">Iron</option><option value="c">Copper</option><option value="w">Wood</option></select></td></tr>');
      if(lastItem && lastItem.length > 0) {
  parent.find(".item:last").after(newItem);
        } else {
          parent.append(newItem);
        }
     fixNewSelectOption(newItem);
     handleDeleteAction();
    });


    $(".addCustomer").on('click', function(e) {
      var newCustomer = $('.customer:last').after('<tr class="customer"><td>New Customer</td><td></td><td></td><td></td></tr>');
      var newCart = $('.cart:first').clone(true, true).wrap('td');
      newCart.find('tbody').html("").html('<tr class="item"><td><button>Delete</button></td><td>New item</td><td></td><td></td><td></td><td><select name="" id="" class="itemType"><option value="-1"></option><option value="i">Iron</option><option value="c">Copper</option><option value="w">Wood</option></select></td></tr>');
      $('.customer:last').append(newCart);
      handleDeleteAction();
      e.preventDefault();
    });


$(document).on('click', '.item button', function(){
  if($(this).closest('table').find('.item button').length > 1) {
    $(this).closest('tr').remove();
  }
  handleDeleteAction();
});

$(document).on('change', '.itemType', function(){
  fixSelectOption($(this));
});

function handleDeleteAction() {
  $('table.cart').each(function(){
    var cart = $(this);
    var deleteButtons = cart.find('.item button');
    deleteButtons.prop('disabled', deleteButtons.length <= 1);
  });
}

function fixSelectOption(elm) {
  var selected = elm.val();
  var options = elm.find('option[value!="' + selected + '"]');
  var elms = elm.closest('table').find('.itemType').not(elm);
  
  elms.each(function(){
    
    var current = $(this);
    if(current.val() === selected){
        current.val('-1');
      }

    options.each(function(){
       var optValue = $(this).val();
       if(optValue !== '-1' && !$(this).prop('disabled')){
        current.find('option[value="' + optValue + '"]').prop('disabled', false);
       }
     });
      
    if(selected !== '-1'){
      current.find('option[value="' + selected + '"]').prop('disabled', true);
     }
  });
}

function fixNewSelectOption(elm) {
  var elms = elm.closest('table').find('.itemType').not(elm);
  elms.each(function(){
    
    var current = $(this);
    if(current.val() !== '-1'){
        elm.find('option[value="' + current.val() + '"]').prop('disabled', true);
      }
            
  });
}
&#13;
table,
        td,
        th {
          border: 1px solid black;
        }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
      <meta name="viewport" content="width=device-width,initial-scale=1, user-scalable=no">
      <title>PO</title>

      <header>
        <form id="panel">
    
        </form>
      </header>
      <section id="I">


    <table id='PO1' class="purchaseOrder">
      <thead>
        <tr>
          <th>Name</th>
          <th>Age</th>
          <th>Address</th>
          <th>Gender</th>
          <th>Cart</th>
        </tr>
      </thead>
      <tbody>
        <tr id='C1' class='customer'>
          <td>Some Name</td>
          <td>30</td>
          <td>My Address</td>
          <td>Male</td>
          <td>
            <table class='cart'>
              <thead>
                <tr>
                  <th>Delete</th>
                  <th>Brand</th>
                  <th>Quantity</th>
                  <th>Size</th>
                  <th>Color</th>
                  <th>Type</th>
                </tr>
              </thead>
              <tbody>
                <tr id='item1' class='item'>
                  <td><button disabled>Delete</button></td>
                  <td>Yamaha</td>
                  <td>30</td>
                  <td>Large</td>
                  <td>Black</td>
                  <td>
                    <select id="select" class="itemType">
                        <option value="i">Iron</option>
                        <option value="c">Copper</option>
                        <option value="w">Wood</option>
                    </select>
                  </td>
                </tr>
              </tbody>
              <tfoot>
                <tr>
                  <td colspan='5'></td>
                  <td>
                    <button class="addItem">Add Item</button>
                  </td>
                </tr>
              </tfoot>
            </table>

          </td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td colspan='3'></td>
          <td>
            <button class="addCustomer">Add Customer</button>
          </td>
        </tr>
      </tfoot>
    </table>
  </section>
      <footer>&nbsp;</footer>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

为了删除行,我添加了checboxes。 PLUNKER

table {
  table-layout: fixed;
}
table,
td,
th {
  border: 1px solid black;
}
.name,
.brand,
.color {
  width: 10%;
}
.age,
.address,
.pay,
.qty,
.size {
  width: 5%;
}
.cart th:first-of-type {
  width: 2.5%;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1, user-scalable=no">
  <title>PO</title>
  <link href="style.css" rel="stylesheet">

</head>

<body>
  <header>

  </header>
  <section id="I">

    <table id='PO1' class="purchaseOrder">
      <thead>
        <tr>
          <th class='name'>Name</th>
          <th class='age'>Age</th>
          <th class='address'>Address</th>
          <th class='pay'>Pay</th>
          <th>Cart</th>
        </tr>
      </thead>
      <tbody>
        <tr id='C1' class='customer'>
          <td>Jon Doe</td>
          <td>30</td>
          <td>Earth</td>
          <td>Credit</td>
          <td>
            <table class='cart'>
              <thead>
                <tr>
                  <th>&nbsp;</th>
                  <th class='brand'>Brand</th>
                  <th class='qty'>Qty</th>
                  <th class='size'>Size</th>
                  <th class='color'>Color</th>
                </tr>
              </thead>
              <tbody>
                <tr id='item1' class='item'>
                  <td>
                    <input class="chx" name="chx" type="checkbox">
                  </td>
                  <td>New Item</td>
                  <td></td>
                  <td></td>
                  <td>
                    <select name='color'>
                      <option value='black'>Black</option>
                      <option value='white'>White</option>
                    </select>
                  </td>
                </tr>
              </tbody>
              <tfoot>
                <tr>
                  <td>
                    <button class="remItem" onclick='remItem(this)'>-</button>
                  </td>
                  <td colspan='3'></td>
                  <td>
                    <button class="addItem" onclick='addItem("itemTemplate", this)'>+</button>
                  </td>
                </tr>
              </tfoot>
            </table>


          </td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td colspan='3'></td>
          <td>
            <button class="addCustomer">Add Customer</button>
          </td>
        </tr>
      </tfoot>
    </table>


  </section>
  <footer>
    <template id="cartTemplate">

      <table class='cart'>
        <thead>
          <tr>
            <th>&nbsp;</th>
            <th class='brand'>Brand</th>
            <th class='qty'>Qty</th>
            <th class='size'>Size</th>
            <th class='color'>Color</th>
          </tr>
        </thead>
        <tbody>
          <tr class='item'>
            <td>
              <input class="chx" name="chx" type="checkbox">
            </td>
            <td>New Item</td>
            <td></td>
            <td></td>
            <td>
              <select name='color'>
                <option value='black'>Black</option>
                <option value='white'>White</option>
              </select>
            </td>
          </tr>
        </tbody>
        <tfoot>
          <tr>
            <td>
              <button class="remItem" onclick='remItem(this)'>-</button>
            </td>
            <td colspan='3'></td>
            <td>
              <button class="addItem" onclick='addItem("itemTemplate",this)'>+</button>
            </td>
          </tr>
        </tfoot>
      </table>

    </template>
    <template id='itemTemplate'>

      <tr class="item">
        <td>
          <input class="chx" name="chx" type="checkbox">
        </td>
        <td>New Item</td>
        <td></td>
        <td></td>
        <td>
          <select name="color">
            <option value="black">Black</option>
            <option value="white">White</option>
          </select>
        </td>
      </tr>

    </template>

  </footer>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

  <script>
    $(".addCustomer").on('click', function(e) {
      var newCustomer = $('.customer:last').after('<tr class="customer"><td>New Customer</td><td></td><td></td><td></td></tr>');
      var newCart = newClone('cartTemplate');
      $('.customer:last').append(newCart);
      e.preventDefault();

    });

    function newClone(id) {
      var template = document.getElementById(id);
      var clone = document.importNode(template.content, true);
      return clone;
    }

    function addItem(id, origin) {
      var newItem = newClone(id);
      $(origin).closest('table').append(newItem);
    }

    function toggle(origin) {
      var chxList = $(origin).closest('table').find('.chx:checked');
      chkList.click(function() {
        chkList.prop('checked', origin.checked)
      })
    }

    function remItem(origin) {
      var chxList = $(origin).closest('table').find('.chx:checked');
      chxList.each(function(idx, obj) {
        obj.closest('.item').remove();
      });
    }
  </script>
</body>


</html>

答案 4 :(得分:0)

我知道您的问题得到了回答,但我无法注意到代码中的一些错误模式。您可能听说过最佳实践短语以及如何以正确的方式完成工作。我想提出一个建议。尽可能避免在Javascript代码中使用HTML,因为它会使应用程序的结构不够好。

我想向您介绍模板

示例我们如何优化您的代码(来自已接受的答案):
将所有内容包裹在newItem标记中的script变量中并放在您身体的某个位置:

<script id="newItem-template" type="text/x-custom-template">
  <tr class="item"><td><button>Delete</button></td>
  <td>New item</td><td></td><td></td><td></td><td>
  <select name="" id="" class="itemType"><option value="-1"></option>
  <option value="i">Iron</option><option value="c">Copper</option>
  <option value="w">Wood</option></select></td></tr>
</script>

浏览器会忽略这一点,直到您调用html()方法返回该内容。

所以不要这样:

 var newItem = $('<tr class="item"><td><button>Delete</button></td><td>New item</td><td></td><td></td><td></td><td><select name="" id="" class="itemType"><option value="-1"></option><option value="i">Iron</option><option value="c">Copper</option><option value="w">Wood</option></select></td></tr>');

在你的javascript文件中,你只有这个:

 var newItem  = $('#newItem-template').html();

以下是优化的工作代码:jsFiddle

这是jQuery管理模板的方法,但如果你的项目变大,有强大的模板引擎:
JavaScript templating engines