簡體   English   中英

如何讀取作為內部html動態插入到表格單元格的html表格內部復選框的值?

[英]How to read value of check box inside html table which was inserted dynamically into the table cell as inner html?

我使用Java腳本動態地向html表的每一行添加了多個復選框(使用“ for”循環,為了便於理解,在代碼部分中省略了“ for”循環)。 代碼是這樣的,

var table = document.getElementById("verifyTable");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
cell1.innerHTML = "<input type='checkbox' class = 'checkSensors' id='checkSensorsId' value=''>";

然后,在運行時,選中/取消選中該復選框時,將使用此類JQuery觸發事件,

$(document).on('change', '[type=checkbox]', function() {
    $("table tr td:nth-child(1)").each(function () {
        var checkbox = $(this).html();
        alert(checkbox);
    });
});

不幸的是,內部html文本<input type='checkbox' class = 'checkSensors' id='checkSensorsId' value=''>是我收到的輸出。 我不知道如何獲取innerhtml“ input”元素的值的方法。 請幫我解決這個問題。

使用is()方法。

 $(document).on('change', '[type=checkbox]', function() { var checkbox = $(this).is(':checked'); alert(checkbox); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='checkbox' class = 'checkSensors' id='checkSensorsId' value='vv'/> 

根據注釋中的建議,對for選擇器進行了一些更改,並使它起作用。

$(document).on('change', '[type=checkbox]', function() {
    $("table tr td:nth-child(1) input").each(function() {
        alert($(this).is(":checked"));
    });
});

暫無
暫無

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

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