开发者

Trouble selecting an item in a SELECT box with jQuery with a quot

I am having a hidden form with a list which I need to select an option from when a user wants to view this form. I was using var assignee = 'persons name';

jQuery('#edit-form' option[value='+option+']:first').attr("selected", "selected");

I tried doing this, but this is not working as the value in the option tag is not specified with the \ as well, so it won't find a match

jQuery('#edit-form option[value='+option.replace(/\'/,'\\\'')+']:first').attr("selected", "selected");

So is doing it this way the following my only option ?

jQuery('#edit-form option').each(function(){
  if(jQuery(this).val() == option) {        
   jQuery(this).attr("selected", "selected");
   return false;
     }
});

a Test page

<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

  <script>
  //test vars
  var option1 = "option two";
  var option2 = "option '";
  var option = option2;

  $(document).ready(function(){
    $('#edit-form [name=select1] option').each(function(){
      if($(this).val() == option) {        
    $(this).attr("selected", "selected");
    return false;
       }
    });

    $('#edit-form [name=select2] option[value='+option+']:first').attr("selected", "selected");
    option = option.replace(/\'/,'\\\'');
    $('#edit-form [name=select3] option[value='+option+']:first').attr("selected", "selected");
  });
  </script>
</head>
<body>
  <form id="edit-form">
  <select name=select1>
  <option value="option one">option one</option>
  <option value="option two">option two</option>
  <option value="option two">option two</option>
  <option value="option \'">option \'</option>
  <option value="option '">option '</option>
  </select>
  <select name=select2>
  <option value="option one">option one</option>
  <option value="option two">option two</option>
  <option value="option two">option two</option>
  <option value="option \'">option \'</option>
  <option value="option '">op开发者_JS百科tion '</option>
  </select>  
  <select name=select3>
  <option value="option one">option one</option>
  <option value="option two">option two</option>
  <option value="option two">option two</option>
  <option value="option \'">option \'</option>
  <option value="option '">option '</option>
  </select>
</body>
</html>


What about:

jQuery('#edit-form option[value="'+option+'"]:first').attr("selected", "selected");

Note the use of the " and ' characters.

E.g. These are all valid strings:

"Valid 'string'"
'valid "string"'
// also valid with escape chars
"valid \"string"
'valid \'string'

but not these

"not valid "string""
'not valid 'string''

You can also see this with the syntax highlighter.

Edit:

Works for me if I use this:

$('#edit-form select[name="select3"] option[value="'+option+'"]').first().attr("selected", "selected");

Note: I use .first() instead of :first and proper select[name="select3"]. Make sure that you use proper jQuery selectors.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜