如何在单击特定按钮时删除特定文本框?

时间:2015-07-31 06:37:50

标签: jquery

$("#minus" + data1[i].PhoneNo[j] + "").on("click", function () {
    $("#textbox" + data1[i].PhoneNo[j] + "").find("input").remove();
});

当我尝试单击文本框旁边的按钮时,我无法删除该特定文本框。任何人都可以帮我解决问题。

先谢谢你。

1 个答案:

答案 0 :(得分:2)

如果你能分享你的HTML代码片段会更好,那么构建js代码会更容易,无论如何这里是为你做的插图:

Html (假设所有这些都是由循环过程创建的)

// put class name on button for references
// 1st group
<input type="text" class="a" value="a"><button class="removeBtn">Remove</button>
<input type="text" class="a" value="b"><button class="removeBtn">Remove</button>
<input type="text" class="a" value="b"><button class="removeBtn">Remove</button>

// 2nd group
// this code wrapped inside parent container
<hr/>
<div class="container">
  <input type="text" class="a" value="a" />
  <button class="removeBtn2">Remove</button>
</div>
<div class="container">
  <input type="text" class="a" value="b" />
  <button class="removeBtn2">Remove</button>
</div>
<div class="container">
  <input type="text" class="a" value="c" />
  <button class="removeBtn2">Remove</button>
</div>

<强>的jQuery

// for first group of textbox
$(document).on('click', '.removeBtn', function() {   
   // Must be noted that, textbox must be aside with button
   // that why we asked for HTML snippet 
   $(this).prev().remove().end().remove();
   // or $(this).prev('.a').remove().end().remove();     
});

// for second group of textbox
$(document).on('click', '.removeBtn2', function () {
  // just remove it parents
  $(this).closest('.container').remove(); 
});

<强> DEMO