jQuery remove selected option from this

大城市里の小女人 提交于 2019-12-03 10:38:46

问题


first post here, I come in peace :) I've searched but can't quite find what I'm after.

I am trying to manipulate the selected option of a select box. Can someone please explain why this works:

$('#some_select_box').click(function() {
  $('#some_select_box option:selected').remove();
});

but this doesn't:

$('#some_select_box').click(function() {
  $('this option:selected').remove();
});

I just want to use "this" instead of spelling out the id of the select box - can someone point me in the right direction for the correct syntax? It's driving me mad because it looks like it should be really simple. And I'm sure it is to someone, but not me, cos its the end of the day and I'm brain-fried... Any pointers much appreciated.

Cheers


回答1:


this isn't a css selector. you can avoid spelling the id of this by passing it as a context:

$('option:selected', this).remove();

http://api.jquery.com/jQuery/




回答2:


 $('#some_select_box').click(function() {
     $(this).find('option:selected').remove();
 });

Using the find method.




回答3:


This should do the trick:

$('#some_select_box').click(function() {
  $('option:selected', this ).remove();
});



回答4:


This is a simpler one

$('#some_select_box').find('option:selected').remove().end();


来源:https://stackoverflow.com/questions/2494191/jquery-remove-selected-option-from-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!