簡體   English   中英

如何在HTML中的JavaScript中綁定動態變量

[英]How to bind Dynamic variable in JavaScript in HTML

我應該如何將在變量@ item.ArticleId中獲取的ID 綁定到動態獲取ID的JavaScript代碼中?

我的用例是,我必須在單擊“ 添加評論”按鈕時顯示“ 評論”文本框

以下是有關如何獲取商品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>

以下是我的JavaScript代碼:

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

這是我解決的方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM