1

我正在尝试将数组中的值与 jquery 组合框中的值进行比较,但没有成功。

我有一个这样的数组:(值1,值2,...)名称用,(示例:)分隔john smith,peter pan,。另一方面,具有来自本地存储的值的组合框以这种方式动态附加

 $('select[name="selectListMenu1"]').append( new Option(nombre.fname + " " + nombre.lname) );

这给了我一个像“John smith”这样的最终名称,我将它附加到组合中。

现在我想将数组中的每个名称与组合框中的每个名称进行比较,并在组合框中选择匹配的名称。

var summary3 = (elnombre.proycontac).split(","); // split the names to compare
var p1 = summary3.length                         // get the number of names to compare
$('[name="selectListMenu1"] ').each(function(){           //from here i get lost 
       for (a=0; a<=p1-2; a++) { 
              if ($(this).text())=== summary3[a] {
              // select the name in combobox
              }
       }
}); 
4

2 回答 2

1

你可以试试这个:

$.map(summary3, function(elem, i) {
  $('select[name="selectListMenu1"] option[text="' + elem + '"]').attr('selected', true);
});

我在这里举个例子。

于 2012-10-23T12:16:53.010 回答
0

如果匹配文本,则设置 selected 属性:

if ($(this).text() === summary3[a]) {
    $(this).attr('selected', true);
}
于 2012-10-23T11:49:24.423 回答