代码之家  ›  专栏  ›  技术社区  ›  Jatin Bhatia

如何在HTML中用JavaScript绑定动态变量

  •  0
  • Jatin Bhatia  · 技术社区  · 7 年前

    如何绑定变量中的Id @item.ArticleId项目

    我的用例是我必须显示 点击 添加注释按钮

    下面是关于如何获取文章ID的代码:

    <span id="addComment" onclick="AddaComment()"><i class="fa fa-comment" aria-hidden="true" ></i> Add a Comment</span>
    
                <div id="Comment(@item.ArticleId)" style="display:none;">
    
                    <input type="hidden" name="ArticleId" id="ArticleId" />
                    <input type="text" name="Comments" />
                    <input type="submit" class="btn add-comment" value="Add Comment">
                </div>
    

    function AddaComment() {
            var x = document.getElementById("Comment()");
            if (x.style.display === "none") {
                x.style.display = "block";
            } else {
                x.style.display = "none";
            }
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Jatin Bhatia    7 年前

    我就是这样解决的:

    function AddaComment(id) {
            var x = document.getElementById("Comment_"+id);
            if (x.style.display === "none") {
                x.style.display = "block";
            } else {
                x.style.display = "none";
            }
        }