插入后如何选择<option>?

时间:2018-11-07 00:38:21

标签: jquery

我要添加一个带有ajax的<option>并将其附加到成功后:

    success: function(data) {
      var newdata = data.replace(/['"]+/g, '');
      jQuery('#argomenti').append('<option value="">'+newdata+'</option>');
    }

但是我将如何选择它,因为其中会有许多其他标签和值?

类似$('#argomenti').val(newdata).prop('selected', true);

2 个答案:

答案 0 :(得分:1)

您只需设置“ selected”属性,如下所示:

jQuery('#argomenti').append('<option value="" selected="selected">'+newdata+'</option>');

答案 1 :(得分:1)

我建议:

// creating the element, passing an object of
// properties, and values, to set to that object:
$('<option>', {
  'selected': true,
  'text': newdata})
// appending the created element to its parent element:
.appendTo('#argomenti');
相关问题