Why does IE8 fail to resolve my JQuery selector fo

2019-07-18 14:15发布

问题:

This following line works as expected in Firefox but IE returns 'undefined'.

var selectedChoice = $("input[name='my.test[0].SelectedOption']:checked", '#myForm').val();

Here is a standalone sample that you can run...

Note the problem seems to be the use of '.' and '[]' in the name of the radio element. However this is how ASP.NET MVC renders them. Here is an example which works fine in Firefox but fails in IE.

<html>
<head>
  <title>Testing radio selection jquery in IE 8</title>
  <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js" type="text/javascript"></script>
</head>
<body>
  <form id="myForm">
      <input name="selected.Option[0]" id="selected_Option1" type="radio" value="1" checked="checked" />
      <label for="selected_Option1">Option 1</label>
      <br />

      <input name="selected.Option1" id="selected_Option2" type="radio" value="2" checked="checked" />
      <label for="selected_Option2">Option 2</label>
      <br />

      <input name="selectedOption[2]" id="selected_Option3" type="radio" value="3" checked="checked" />
      <label for="selected_Option3">Option 3</label>
      <br />

      <input name="selectedOption3" id="selected_Option4" type="radio" value="4" checked="checked" />
      <label for="selected_Option4">Option 4</label>
      <br />

      <span id="displaySelectedChoice1">No value selected.</span>
      <br />
      <span id="displaySelectedChoice2">No value selected.</span>
      <br />
      <span id="displaySelectedChoice3">No value selected.</span>
      <br />
      <span id="displaySelectedChoice4">No value selected.</span>
  </form>

  <script language="javascript" type="text/javascript">
    var selectedChoice = $("input[name='selected.Option[0]']:checked").val();
    $('#displaySelectedChoice1').html('You have selected: ' + selectedChoice);

    var selectedChoice = $("input[name='selected.Option1']:checked").val();
    $('#displaySelectedChoice2').html('You have selected: ' + selectedChoice);

    var selectedChoice = $("input[name='selectedOption[2]']:checked").val();
    $('#displaySelectedChoice3').html('You have selected: ' + selectedChoice);

    var selectedChoice = $("input[name='selectedOption3']:checked").val();
    $('#displaySelectedChoice4').html('You have selected: ' + selectedChoice);
  </script>
</body>
</html>

Notice that the 2nd, 3rd and 4th all work but the 1st returns 'undefined' for IE. Its the only one with both '.' and '[]'.

Thanks

回答1:

Try to escape the square braces in the selector, I don't think IE likes 'em too much. Also, you forgot to close the attribute selector.

var selectedChoice = $('input[name="my.test[0].SelectedOption"]:checked', '#myForm').val();

Or with escaped square braces and periods:

var selectedChoice = $('input[name="my\\.test\\[0\\]\\.SelectedOption"]:checked', '#myForm').val();

Yes, there are 2 backslashes.



回答2:

I think this will work:

var selectedChoice = 
    $('input[name="my.test[0].SelectedOption"]:checked', '#myForm').val();

UPDATE:

Replace your JS with this:

var $radio = $('input[name="my\\.test\\[0\\]\\.SelectedOption"]');

$radio.change(function() {

    $("#displaySelectedChoice")
        .html("You have selected: " + $radio.filter(":checked").val());  

});

See working jsFiddle demo



回答3:

Your select test is invalid javascript:

var selectedChoice = $('input[name='my.test[0].SelectedOption:checked', '#myForm').val();
                                   ^--embeded quote breaks the string

This should work better:

var selectedChoice = $("input[name='my.test[0].SelectedOption']:checked", '#myForm').val();

Note that the :checked portion is not in the name= portion



回答4:

I think you need to escape those [] inside your selector:

var selectedChoice = $("input[name='my.test\\[0\\].SelectedOption']:checked", '#myForm').val();

http://api.jquery.com/category/selectors/

At the top it has an entire paragraph about escaping.



回答5:

I find your code pretty complicaded. Try something like this:

<form id="myForm">
      <input name="my.test[0].SelectedOption" class="test" type="radio" value="1"/>
      <label for="my_test_0__SelectedOption_1">Option 1</label>
      <br />

      <input name="my.test[0].SelectedOption" class="test" type="radio" value="2"/>
      <label for="my_test_0__SelectedOption_1">Option 2</label>
      <br />

      <input name="my.test[0].SelectedOption" class="test" type="radio" value="3"/>
      <label for="my_test_0__SelectedOption_1">Option 3</label>
      <br />
      <span id="displaySelectedChoice">No value selected.</span>
  </form>

  <script language="javascript" type="text/javascript">
     $(".test").change(
       function () {
         var value = $(this).val();
         $("#displaySelectedChoice").replaceTo("you choose: "+value)
       }
     )
  </script>


回答6:

Dont know wich version of jquery youre using, but even though I red about the new version of jquery that IE has a problem to "see" what you want with this ":checked". Anyways avoid using this atribute for radio because, well the only checked radio is the one changed anyways. My point is make a simpler code, it might help.