【问题标题】:How to find element from html with jQuery如何使用jquery从html中查找元素
【发布时间】:2022-01-26 08:04:17
【问题描述】:

我有一个表格,每行都有复选框选项。 对于每一行,我都有由 HTML 文件处理的 href(专用链接)。

我创建了一个由JS调用的按钮:“下载”,我想定义这个按钮来保存复选框中选择的行的所有链接,并将其保存在一个变量中,selectedLinks = []

知道如何修复我的 JS 函数。

//function download button

$(document).on("click", "#downloadBtn", function() {
console.log("download button click")
var selectedLinks = []
$('.last')
    .checkSingel("checkSingle")
    .forEach(function(record) {
        if (record.checked) {
            //selectedLinks.push(record.href)
            alert($(this).attr("href"));
        }
    })
console.log(selectedLinks)
});
</td>
<td class="last"><a style="font-weight:bold;text-decoration:underline;" href="/download/{{ elements.EvidencePath }}">Click here to download Evidence</a>
</td>

【问题讨论】:

  • 能否请您添加正确的代码,以便我更好地理解。
  • 你会在jsfiddle.netcodepen.io上添加代码sn-p

标签: javascript html jquery


【解决方案1】:

这是正确的代码:

        // function to add multiple check box for all rows

        $(document).on("click", ".checkAll", function(){
           $('input:checkbox').not(this).prop('checked', this.checked);
           // this statement is checked it will enable the download button ( for check all button)
           if(this.checked) {

               $(".dt-buttons a:last-child").removeAttr("disabled");
           }else {
            $(".dt-buttons a:last-child").attr("disabled", "disabled");
           }
        });
        
        // this statement is checked to eanbled the  button if the checkbox checked. ( for check single button)
        $(document).on("click", ".checkSingle", function(){

           if(this.checked) {

               $(".dt-buttons a:last-child").removeAttr("disabled");
           }else {
            $(".dt-buttons a:last-child").attr("disabled", "disabled");
           }
        });

//function download button

            $(document).on("click", "#downloadBtn", function(){
                console.log("download button click")
                var selectedLinks = []

            $('.last').checkSingel("checkSingle").forEach(function(record){
                if (record.checked){
                    //selectedLinks.push(record.href)
                    alert($(this).attr("href"));
                }
            })
                console.log(selectedLinks)
        });

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2012-04-18
    • 2021-06-05
    • 1970-01-01
    相关资源
    最近更新 更多