0

这是我的代码(仅此而已)

$(document).ready(function() {
        $(".system").click(function() {
            var sid = this.id;
            alert(sid); // Outputs correct id
            $("#graph").html('<img src="graph.php?system=' + sid + '" />'); // sid seems to be null (call looks like "http://host/grap.php?system=")
        })
    })

显然,警报sid有效,而sid在选择器的 html 方法中使用则无效。我也尝试将值写入表单输入,然后从那里读取,但这也不起作用。

我认为,html 方法不支持其他调用或变量访问。对此有什么解决方法?

4

1 回答 1

4

注意:问题现已被编辑,因此在问题的示例代码中不再明显。


您正在src提前关闭属性值。将其更改为:

$("#graph").html('<img src="graph.php?system=' + sid + '" />');
                                          //^Removed " here

您当前拥有的内容将导致如下标记:

<img src="graph.php?system="something" />

从中可以看出,src属性值只有graph.php?system=,这正是你所说的你注意到的。

通过更改上述行,您将获得如下标记:

<img src="graph.php?system=something" />
于 2012-06-08T10:12:19.230 回答