我有一個選項字段默認情況下2選項。我有一個按鈕「添加更多選項」,這將動態添加一個輸入字段(包含減號按鈕)。獲取按鈕的ID刪除輸入字段上的減號按鈕
我想在用戶單擊減號按鈕時刪除特定的輸入字段。
<div class="col-sm-4 col-sm-offset-4">
<form class="form-horizontal" action="index" method="post">
<div id="optionsList"class="form-group">
<label for="options">Options</label>
<input type="text" class="form-control" id="option1" placeholder="M S Dhoni">
<input type="text" class="form-control" id="option2" placeholder="Virat Kohli">
</div>
<div class="form-group">
<button type="button" class="btn btn-lg btn-default" id="moreOptions">Add More Options</button>
</div>
</form>
</div>
這裏是腳本 -
$(document).ready(function(){
var optionNo = 2;
$("#moreOptions").click(function(){
optionNo = optionNo + 1;
$("#optionsList").append
('<div class="input-group" id="inputField' + optionNo +'"><input type="text" class="form-control" placeholder="New option field" id="option' + optionNo +'"><i class="input-group-btn"><button class="btn btn-danger" type="button" id="removeBtn' + optionNo +'"><i class="glyphicon glyphicon-minus"></i></button></i></div>');
});
// here i want to write code to remove that input field
});
我想這一點 -
$(".input-group").click(function(){
const id = this.id;
$("#id").remove();
});
請建議。任何其他方式來做到這一點也沒有問題。
你應該保持HTML根據需要嘗試沒有工作的問題 –