gpt4 book ai didi

javascript - 访问jquery中设置的所有输入元素值属性

转载 作者:行者123 更新时间:2023-12-02 21:48:59 25 4
gpt4 key购买 nike

我想访问所有具有 type="date"和日期值集的输入标签

let matchedelem = $('input[type="date"]').html();  
alert(matchedelem.length);

无论是否设置日期,都会向所有输入元素发出警报

我的要求是我想选择所有设置了日期值的输入元素,如何修改jquery选择器?

let buttonsdiv;

function maintest() {
constructTable();
getDOMButtons();
}

function constructTable() {
table = $('<table>');
let row;
let cell1;
let cell2;
let header2;

table.attr({
"id": "testTable"
});


row = $('<tr>');
table.append(row);

header2 = $('<th>').text("Feature");
header3 = $('<th>');
input = header3.text("Return Date/Time");
header3.append(input);
row.append(header2);
row.append(header3);

for (i = 0; i < 3; i++) {
row = $('<tr>');
table.append(row);


cell1 = $('<td>').html("cell1");
row.append(cell1);

cell2 = $('<td>');
row.append(cell2);

input = $('<input>').attr({
"type": "date",
"id": "input" + i
});
cell2.append(input);
}

$("#mainDiv").append(table);
}


function getDOMButtons() {

buttonsdiv = $("<div></div>").attr({
"id": "buttonsdiv"
});
$("<button>Save</button>").attr({
"value": "Save",
"id": "saveButton"
})
.appendTo(buttonsdiv).click(function() {
parseDOM();
});

$("#mainDiv").append(buttonsdiv);
}

function parseDOM() {
$(document).ready(function() {
let divs = $('input[type="date"]');
alert(divs.length);
})
}
table {
display: unset !important;
border-collapse: collapse;
}

td,
th {
border: 1px solid black;
padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body onload="maintest()">
<div id="mainDiv"></div>
</body>

最佳答案

试试这个:

$(function() {
$('button').click(function() {
$('body').find("input[type='date']").each(function(i) {
console.log($(this).val());
});
});
});
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
#1 <input type="date" name="date1" /><br> #2 <input type="date" name="date2" /><br> #3 <input type="date" name="date3" /><br>
<button>Submit!</button>
</body>

关于javascript - 访问jquery中设置的所有输入元素值属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60175842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com