简体   繁体   English

如何更改日期输入的 innerHTML 以使该值与当前用户输入的值匹配?

[英]How do I change the innerHTML of a date input to make the value match the currently user-entered value?

I am currently working on exporting a table by grabbing the innerHTML of each row of the table.我目前正在通过抓取表格每一行的innerHTML来导出表格。 I have date inputs that user can change and some are auto-generated.我有用户可以更改的日期输入,有些是自动生成的。 Regardless, this obviously does not change the innerHTML and therefore when I grab the html to export the table, the current values are not reflected.无论如何,这显然不会改变innerHTML,因此当我抓取html来导出表格时,当前值不会被反映。 I want to change the innerHTML of these elements so the value as described in the HTML is consistent with what is currently on the page.我想更改这些元素的innerHTML,以便HTML 中描述的值与当前页面上的值一致。

Example of an element in the table whose innerHTML I want updated:我想要更新其innerHTML的表中元素的示例:

<td style="padding-left:1.5em !important;">
<div class="input-group date">
<input Name="exampleinput" class="form-control" id="exampleid" readonly="true" type="date" value="" />
</div>
</td>

I want to have the value of this updated when I go to export the table.我想在导出表时更新此值。

 $('table td:has(input[type!="hidden"])').each(function () {
 // ???????

Reproduce table:重现表:

 function fnExcelReport() { var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>"; var textRange; var j = 0; tab = document.getElementById('thetable'); // id of table $('table td:has(input[type!="hidden"])').each(function() { //$(this).html($('input[type!="hidden"]', this).val()); //currently just replaces entire html with just text }); for (j = 0; j < tab.rows.length; j++) { var testing = tab.rows[j].innerHTML; tab_text = tab_text + tab.rows[j].innerHTML + "</tr>"; tab_text = tab_text + "</tr>"; } tab_text = tab_text + "</table>"; var ua = window.navigator.userAgent; var msie = ua.indexOf("MSIE "); if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\\:11\\./)) // If Internet Explorer { txtArea1.document.open("txt/html", "replace"); txtArea1.document.write(tab_text); txtArea1.document.close(); txtArea1.focus(); sa = txtArea1.document.execCommand("SaveAs", true, "Say Thanks to Sumit.xls"); } else //other browser not tested on IE 11 sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text)); return (sa); }
 <table id="thetable" class="col-sm-offset-1" style="border: 1px solid #000 !important;"> <thead> <tr> <th scope="col" class="col-sm-1 text-center" style="border: 1px solid #000 !important;">Stage Code</th> <th scope="col" class="col-sm-4 text-center" style="border: 1px solid #000 !important;">Stage</th> <th scope="col" class="col-sm-3 text-right" style="border-left: 1px solid #000 !important;border-bottom: 1px solid #000 !important;min-width:160px;">header</th> <th scope="col" class="col-sm-2" style="border-bottom: 1px solid #000 !important;">header</th> </tr> </thead> <tbody> <tr style="height:1em;"> <td style="border-left: 1px solid #000 !important;border-right: 1px solid #000 !important;"></td> <td style="border-left: 1px solid #000 !important;border-right: 1px solid #000 !important;"></td> <td></td> <td></td> </tr> <tr> <td class="text-center" style="border-left: 1px solid #000 !important;border-right: 1px solid #000 !important;">10.99</td> <td class="text-left" style="border-left: 1px solid #000 !important;border-right: 1px solid #000 !important;padding-left:1.5em !important;">Row title</td> <td style="padding-left:1.5em !important;"> <div class="input-group date"> <input Name="inputname" class="form-control" id="inputid" readonly="true" type="date" value="" /> </div> </td> </tbody> </table>

EDIT: Added (what I hope is) a minimal, reproducible example.编辑:添加(我希望是)一个最小的、可重现的例子。

EDIT 2: Answer below is correct.编辑2:下面的答案是正确的。 I accomplished it via this:我通过这个完成了它:

$('table input[type="date"]').each(function () {
var newvalue = $(this).val();
$(this).attr("value", newvalue);


});

(I'll note that for some reason even though I see the innerHTML being grabbed now has the value fields correct, it isn't showing in my exported Excel document, so I'll have to play around with that a bit.) (我会注意到,出于某种原因,即使我看到现在被抓取的 innerHTML 的值字段是正确的,但它没有显示在我导出的 Excel 文档中,所以我必须稍微处理一下。)

You need to set the defaultValue property.您需要设置defaultValue属性。

https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement : https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

defaultValue |默认值 | string: Returns / Sets the default value as originally specified in the HTML that created this object.字符串:返回/设置最初在创建此对象的 HTML 中指定的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM