使用其他页面内容动态填充正确的DIV

时间:2012-02-27 00:21:14

标签: c# javascript jquery html

我的页面上有多个DIV,而我在使用另一个页面视图填充正确的DIV时遇到问题。我很感激一些帮助。

我有一个填充DIV列表的循环。最后解析的HTML看起来像这样:

<div id="replycontainer:1">
CONTENT1
<button id="to:1" class="reply">REPLY</button>
</div>  

<div id="replycontainer:2">
CONTENT2
<button id="to:2" class="reply">REPLY</button>
</div> 

<div id="replycontainer:3">
CONTENT3
<button id="to:3" class="reply">REPLY</button>
</div>    

现在让我们说用户点击按钮“to:3”,现在我需要在“#replycontainer:3”中添加一个“评论”框。

我的JavaScript,我认为是正确的不起作用。它正确地检测单击的DIV类,但没有其他页面内容添加到选定的DIV。

可能是什么问题?

Javascript / Jquery我用来检测点击了哪个DIV按钮:

<script type="text/javascript">

$(document).ready(function () {
    $("button.reply").click(loadComment);
});


function loadComment() {
    var id = this.id.split('to:');
    var postId = id[1];
    var containername = "#replycontainer:" + postId;

    $(containername).load("/Post/Comment");  //MVC Action pointer
}

</script> 

提前致谢!

4 个答案:

答案 0 :(得分:1)

在这里你引用一个div:

var containername = "#replycontainer:" + postId;

但容器实际上是一个类:

<div class="replycontainer:1>

另外,还有一个错字。你忘了用双引号关闭类令牌。

我在这里修好了:

var containername = ".replycontainer:" + postId;
...
<div class="replycontainer:1">

答案 1 :(得分:1)

每个jQuery选择器API都需要使用“\\”转义“:”。用于分隔类名的错误系统

http://api.jquery.com/category/selectors/

“:”用于定义各种选择器,如“:input”,“:checked”等

答案 2 :(得分:0)

你错过了“”在

class="replycontainer:1

如果是实际代码。我所知道的':'符号在id和class属性中已被弃用,我知道。

答案 3 :(得分:0)

我不认为load()中的值与Javascript / Jquery有关。

$(containername).load("/Post/Comment");  //MVC Action pointer

load()只能在页面加载期间运行一些javascript。但是您尝试在客户端脚本上运行服务器脚本。看看吧。

相关问题