在字符串JQuery中搜索和替换选项元素

时间:2014-05-09 14:54:10

标签: jquery html search replace

我有下一个字符串变量,其中包含下一个字符串

var content = '<div id="TypeModifiedProduct" class="form-group WSUage2">
  <label for="TypeModified">Type</label>
  <span class="pull-right alignText" style="margin-right: 40%;">
    <input type="checkbox" id="UseTypeModified" name="UseTypeModified" > Use option
  </span>
  <select name="TypeModified" id="TypeModified" class="form-control" style="width:40%" >
    <option value="Maintenance - Stand Alone form" >Maintenance - Stand Alone form</option>
    <option value="Maintenance Plus - Stand Alone form">Maintenance Plus - Stand Alone form</option>
  </select>
</div>';

现在我想要搜索第二个选项

<option value="Maintenance Plus - Stand Alone form">Maintenance Plus - Stand Alone form</option>

并将其更改为beign selected option

<option value="Maintenance Plus - Stand Alone form" selected >Maintenance Plus - Stand Alone form</option>

然后最后使用option元素中的更改返回这个新字符串。

感谢您的建议

1 个答案:

答案 0 :(得分:0)

因为你正在寻找一个字符串作为结果

var $div = $('<div />',{html: content});
$div.find('option[value="Maintenance Plus - Stand Alone form"]').attr('selected', 'selected');

content = $div.html();

console.log(content)

演示:Fiddle