我正在尝试使用 cheerio
的 HTML 元素的 text()
属性从选择下拉列表中选择一个选项。到目前为止,我正在做的是尝试将 attr
设置为选定的匹配,即具有特定文本的元素。
这就是我正在尝试的:
$('#ddl_city option[text="testing"]').attr('selected', 'selected');
当我这样做时,我没有对 html 进行任何更改。执行此操作后,我在控制台上打印了 html 文档,但没有看到该选项已更改为我选择的选项。知道为什么不起作用或其他解决方法吗?
最佳答案
你可以试试这个:
$('#ddl_city option').
filter(function () { return $(this).text() == 'testing'; }).
attr('selected', true);
感谢 Godsbest 在 this post 中的回答在 jQuery 论坛上。
关于javascript - 通过文本选择与cheerio(jquery)选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39671714/