我们可以传递eq
中我传递索引的任何条件,它会删除所有下拉列表的所有选项
(大于指数)
https://jsbin.com/raleqolere/edit?html,js,output
$(function(){
$('.ifsc_getbankbtn').click(function(){
var option='<option value="ddd">ss</option>';
//remove all options expect first greater the 1 position
$('.dropdowns_js').eq(1).remove();
})
})
换句话说,我想删除所有选项,首先是data-key =&#34;分区&#34; ,data-key =&#34; branch&#34;
答案 0 :(得分:1)
$(function(){
$('.ifsc_getbankbtn').click(function(){
var option='<option value="ddd">ss</option>';
//which have key`state` append options
$('.dropdowns_js').append(option)
});
$('.ifsc_clearbankbtn').click(function(){
var index = 1; // set index value as per you requirement.
$('.dropdowns_js').each(function(a,b){
if(a > index)
{
var defaultOption=b.options[0];
$(this).html('');
$(this).html(defaultOption);
}
})
})
})
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
</head>
<body>
<div data-dropdown="ifsc_dropdowns">
<div class="ifsc_details">
<form>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="bank">
<option selected="true" disabled="true" value="0">Select Bank</option>
</select>
</span>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="state">
<option selected="true" disabled="true" value="0">Select State</option>
</select>
</span>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="district">
<option selected="true" disabled="true" value="0">Select District</option>
</select>
</span>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="branch">
<option selected="true" disabled="true" value="0">Select Branch</option>
</select>
</span>
<button type="button" data-bankdetail="btnbankdetail" class="ifsc_getbankbtn">Get IFSC code</button>
<button type="button" data-bankdetail="btnbankdetail" class="ifsc_clearbankbtn">Remove IFSC code</button>
</form>
</div>
</div>
</body>
</html>
&#13;
答案 1 :(得分:0)
我认为你想删除所有选项而不是每个选择框中的第一个选项
如下所示: -
$(function(){
$('.ifsc_getbankbtn').click(function(){
var option='<option value="ddd">ss</option>';
//which have key`state` append options
$('.dropdowns_js').append(option)
});
$('.ifsc_clearbankbtn').click(function(){
$('.dropdowns_js').each(function(){
$(this).find("option:gt(1)").remove();
});
})
})
$(function(){
$('.ifsc_getbankbtn').click(function(){
var option='<option value="ddd">ss</option>';
//which have key`state` append options
$('.dropdowns_js').append(option)
});
$('.ifsc_clearbankbtn').click(function(){
$('.dropdowns_js').each(function(){
$(this).find("option:gt(1)").remove();
});
})
})
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
</head>
<body>
<div data-dropdown="ifsc_dropdowns">
<div class="ifsc_details">
<form>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="bank">
<option selected="true" disabled="true" value="0">Select Bank</option>
</select>
</span>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="state">
<option selected="true" disabled="true" value="0">Select State</option>
</select>
</span>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="district">
<option selected="true" disabled="true" value="0">Select District</option>
</select>
</span>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="branch">
<option selected="true" disabled="true" value="0">Select Branch</option>
</select>
</span>
<button type="button" data-bankdetail="btnbankdetail" class="ifsc_getbankbtn">Get IFSC code</button>
<button type="button" data-bankdetail="btnbankdetail" class="ifsc_clearbankbtn">Remove IFSC code</button>
</form>
</div>
</div>
</body>
</html>