.val() 选择文本而不是值字符串,当网页动态编辑源 html

.val() select text instead the value string when the webpage dinamically edits the source html

本文关键字:网页 动态 编辑 html 字符串 选择 文本 val      更新时间:2023-09-26

我在网页上有这个html:

<select name="chapter_select" class="chapter_select">
<option value="http://www.example.com/chapter/1">Chapter 1</option>
<option value="http://www.example.com/chapter/2">Chapter 2</option>
<option value="http://www.example.com/chapter/3">Chapter 3</option>
<option value="http://www.example.com/chapter/4">Chapter 4</option>
<option value="http://www.example.com/chapter/5">Chapter 5</option>
<option value="http://www.example.com/chapter/6" selected="selected">Chapter 6</option>
</select>

而这个变量:

getInformationsFromCurrentPage : function(doc, curUrl, callback) {
//This function runs in the DOM of the current consulted page
var curChapName = $("select.chapter_select:first option:selected", doc).text();
var chapurl = $("select.chapter_select:first option:selected", doc).val();
callback({"name": name, 
        "currentChapter": curChapName, 
        "currentMangaURL": nameurl, 
        "currentChapterURL": chapurl});
},
}

问题是两者都返回相同的文本值。 Chapurl 应该返回 URL,而不是文本值。这一直有效,直到网站修改了他们的网站。

我知道我会很挑剔,但这是开发扩展的手册,文档变量是必须的 http://www.allmangasreader.com/dev.php 只需注意getInformationsFromCurrentPage函数。它使用的是jQuery 1.4.2,所以也许是这样,奇怪的是其他网站只是像它一样工作。

更新:服务器正在使用脚本(重写html),使我认为下拉列表位于顶部。我使用了一些选择器来获得遵循源的正确选择器。

var curChapName = $("select.chapter_select:first option:selected", doc).text();
var chapurl = $("select.chapter_select:first option:selected", doc).val();

这项工作 http://jsfiddle.net/所以你的 doc 变量一定有问题,请检查它

总是可以使用回退并使用 attr。这样,您始终可以保证您将获得价值。

var chapval = $("select.chapter_select:first option:selected").attr('value');

http://jsfiddle.net/dt5PV/

var curChapName = $("select.chapter_select:first :selected").text()
var chapurl = $("select.chapter_select:first").val()