开发者

jQuery .remove() doesn't fire in IE8 [closed]

开发者_如何学Python It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

Who can explain this, please?

In my function, I use .remove() to remove one row of a <select> box (I mean to remove an <option>) but why doesn't it work?

In FF it looks good, but in IE8 ... fail.

Let's see an example:

<form name="test">

I have two <select> boxes, the first is:

<select id='car1' name='car1' onchange='car_remove();'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

and the second is:

<select id='car2' name='car2'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

</form>

including this tiny JavaScript function (with jquery.js include script in header):

function car_remove()
{
  car1_selected = document.test.car1.value;
  $("#car2 option[value="+ car1_selected +"]").remove();
}


I don't have IE8 to test, but a couple of things might be your problem:

1) The jquery value selector should be in quotes. Change

$("#car2 option[value="+ car1_selected +"]").remove();

to

$("#car2 option[value='"+ car1_selected +"']").remove();

Note the addition of single quotes in this example (you can also use escaped double quotes).

2) Get in the habit of using the full javascript selector (or jquery equivalent):

car1_selected = document.test.car1.value;

should be

 car1_selected = document.getElementById("car1").value;

In fact, this might be your problem, as 'test' isn't actually an id, but a name.


function car_remove()
{
  // car1_selected = document.test.car1.value; *Don't use this its a plain javascript*
  // use this. Always try to use jQuery API's instead of conventional JS
  car1_selected =  $('#car1 option[selected]').val();
  $("#car1 option[value="+ car1_selected +"]").remove();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜