javascript - 如何通过javascript获取表格中的所有输入日期格式?
问题描述
我创建一个带有日期输入的 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 - sql更新语句node.js
- c++ - 尝试在 XE 中访问 RAD Studio 10.2 编译库时出现链接错误
- python - 反转集合的元素
- php - 如何使用 Laravel Eloquent 过滤所有和特定字段?
- r - 使用 cross() 对数据框中选定的 col 进行变异
- loops - 如何在 simulink 上构建一个 while 迭代器来更新参数?
- android - 当我打开android studio时,如何摆脱我的MAC上弹出的xcodebuild弹出窗口?
- aem - AEM Slightly - 如何调用名称存储在变量中的 java 方法 - 表达式中的表达式无法正常工作
- java - 从 IBM WebSphere 8.5 迁移到 JBoss EAP 7.0 - SOAP 请求无法解析,'ElementNSImpl 无法转换为 javax.xml.soap.SOAPElement'
- vue.js - 我的 eventBus 实现有什么问题?