使用 jquery 根据输入文本框更改并显示[删除/添加]可用选项
<input id="studentnoofinst" type="text" size="20" value=""/>
<select id="studentinst" size="6">
<option value="1">First Installment</option>
<option value="2">Second Installment</option>
<option value="3">Third Installment</option>
<option value="4">Fourth Installment</option>
<option value="5">Fifth Installment</option>
<option value="6">Sixth Installment</option>
</select>
Jquery
$("#studentnoofinst").keyup(function() {
//$("select option[value='fb']").prop("selected",true);
alert("going to change");
$leap=0;
$leap=($('#studentnoofinst').val());
if($leap!=''){
for(i=$leap;i<=6;i++) {// displaying the no of installments
if(i!=$leap){
$('#studentinst option[value='+i+']').remove();
}
}
}
});
嗨,如上面的代码所示,我能够删除所选的行,我的问题是我无法将所选的选项添加回来,如何将所选的行添加回来 PS:一旦输入4,最后2行将被删除,稍后如果输入5,仅显示前4行,第5行被删除..但
如果第一次输入= 4
则需要它输出:
<option value="1">First Installment</option>
<option value="2">Second Installment</option>
<option value="3">Third Installment</option>
<option value="4">Fourth Installment</option>
如果第二次输入=5
输出:
<option value="1">First Installment</option>
<option value="2">Second Installment</option>
<option value="3">Third Installment</option>
<option value="4">Fourth Installment</option>
<option value="5">Fifth Installment</option>//vomitted but i need this? add
<input id="studentnoofinst" type="text" size="20" value=""/>
<select id="studentinst" size="6">
<option value="1">First Installment</option>
<option value="2">Second Installment</option>
<option value="3">Third Installment</option>
<option value="4">Fourth Installment</option>
<option value="5">Fifth Installment</option>
<option value="6">Sixth Installment</option>
</select>
Jquery
$("#studentnoofinst").keyup(function() {
//$("select option[value='fb']").prop("selected",true);
alert("going to change");
$leap=0;
$leap=($('#studentnoofinst').val());
if($leap!=''){
for(i=$leap;i<=6;i++) {// displaying the no of installments
if(i!=$leap){
$('#studentinst option[value='+i+']').remove();
}
}
}
});
Hi, as shown in the code above i am able to remove the selected row, My question is that i am not able to add the selected option back,How to add the selected row back
P.S:once if enter 4 the last 2 rows are deleted and later if enter 5 only the first 4 rows are display and the 5 row is vomitted.. but it need it
if first time the input =4
ouptut:
<option value="1">First Installment</option>
<option value="2">Second Installment</option>
<option value="3">Third Installment</option>
<option value="4">Fourth Installment</option>
if second time the input =5
ouptut:
<option value="1">First Installment</option>
<option value="2">Second Installment</option>
<option value="3">Third Installment</option>
<option value="4">Fourth Installment</option>
<option value="5">Fifth Installment</option>//vomitted but i need this? add
Live example here: http://jsfiddle.net/PQ7GF/30/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑 - 合并灵魂所做的更改:
您可以将选项保存在变量中并将它们添加回来:
在这里小提琴: http ://jsfiddle.net/PQ7GF/49/
EDIT - merged the changes made by soul:
you could save the options in a variable and add them back:
fiddle here: http://jsfiddle.net/PQ7GF/49/
一旦删除,就删除了!
如果您只想移动一行,您可以查看 jQuery 的 before、after 和append 函数。
如果您想临时删除一行并稍后插入,您应该使用分离。
如果您想隐藏它并稍后再次显示,您应该使用隐藏和显示功能。
前往 jQuery 文档 并查找这些函数以了解哪些函数适合。
这是一个带有 detach() 的小提琴,当重新插入同一元素时,这会保持所有其他数据完好无损: http://jsfiddle .net/PQ7GF/50/
Once it's removed, it's removed!
If your just going to move a row you can look at jQuery's before, after and append functions.
If you would like to temporary remove a row, and insert it later you should use detach.
If you would like to hide it and show it again later you should use hide and show functions.
Head over to the jQuery documentation and look up those functions to see what fits.
Here's a Fiddle with detach(), this keeps all other data intact when the same element is reinserted : http://jsfiddle.net/PQ7GF/50/