4

我有一个包含网络设备列表的 HTML(多)选择列表。

<select id="Printers" multiple="multiple" name="Printers">
  <option value="\\ABCDEF01\PRINTER1\">\\ABCDEF01\PRINTER1\</option>
  <option value="\\ABCDEF02\PRINTER2">\\ABCDEF02\PRINTER2</option>
  ...
  ...
</select>

我想用javascript自动选择值。这是我目前使用的代码:

//Assume: val.Name == "\\ABCDEF02\PRINTER2"

var select = document.getElementById('Printers');
var escapedName = val.Name.replace(/\\/g, "\\\\"); // Escape backslashes.
$("option[value=\"" + escapedName + "\"]", select).attr('selected', 'selected');

此代码适用于 chrome,但不适用于 IE7。有没有我应该知道的怪癖会阻止它在 IE7 上正常工作?

4

1 回答 1

0

似乎工作.. 与 jquery 1.9

http://jsfiddle.net/2nGnm/

html:

<select id="Printers" multiple="multiple" name="Printers">
  <option value="\\ABCDEF01\PRINTER1\">\\ABCDEF01\PRINTER1\</option>
  <option value="\\ABCDEF02\PRINTER2">\\ABCDEF02\PRINTER2</option>
</select>

js:

var select = document.getElementById('Printers');
var escapedName = $('[name="Printers"] option:last').val().replace(/\\/g, "\\\\"); // Escape backslashes.
$('option[value="' + escapedName + '"]').prop('selected', true);
于 2013-01-25T15:04:23.897 回答