我创建了一个带有日期输入的 html 表格:
<table id="myTable">
<tr>
<td><input name="start" type="date" value= ""/> ~ <input name="end" type="date" value=""/></td>
</tr>
<tr>
<td><input name="start" type="date" value= ""/> ~ <input name="end" type="date" value=""/></td>
</tr>
</table>
现在我想通过 javascript 将所有输入值获取到字符串,如下所示:
2019-06-01~2019-06-02,2019-06-03~2019-06-04
我刚刚开始使用 javascript,有人可以帮助我吗?非常感谢!
最佳答案
您可以将输入作为字符串获取,如下所示:
function getDates() {
var res = '';
var rows = $('#myTable').find('tr');
rows.each(function(index, el) {
res += $(el).find('input[name=start]').val();
res += '~';
res += $(el).find('input[name=end]').val();
if (index < rows.length - 1) {
res += ', ';
}
});
return res;
}
console.log(getDates());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="myTable">
<tr>
<td><input name="start" type="date" value="2018-01-23" /> ~ <input name="end" type="date" value="2019-01-22" /></td>
</tr>
<tr>
<td><input name="start" type="date" value="2018-01-23" /> ~ <input name="end" type="date" value="2019-01-28" /></td>
</tr>
</table>
关于javascript - 如何通过javascript获取表中所有输入的日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56423284/