JavaScript 而不是 jQuery 表单提交()

JavaScript instead of jQuery form submit()

我正在尝试将页面从 jQuery 转换为纯 JavaScript。

"submit" 是问题所在。能想到的我都试过了

切换到 JS 后,我无法使以下行工作:

我尝试将其切换为:

上下文中的代码如下。

jQuery:

<script type="text/javascript">
function excel( )
{
 $( '#dataToDisplay' ).val( $( '#jfabtable' ).html( ) );  // replacing with JavaScript
}
</script>

<!-- the following line works, but is jQuery -->
<a href='javascript:void(0);' onclick='$("#savetoexcelform").submit();'><img src='./excel.png' width='48' height='48' border='0' title='Download to Excel' alt='Download to Excel' /></a>

<form action='convert.php' name='savetoexcelform' id='savetoexcelform' method='post' target='_blank' onsubmit='return excel( );'>
 <input type='hidden' id='dataToDisplay' name='dataToDisplay'>
 <input type='hidden' id='filename' name='filename' value='output.xls'>
</form>

JavaScript:

<script type="text/javascript">
function excel( )
{
 document.getElementById( 'dataToDisplay' ).value = document.getElementById( 'jfabtable' ).innerHTML;  // replaced, tested and works
}
</script>

<!-- the following line is JS, but doesn't work -->
<a href='javascript:void(0);' onclick='document.getElementById( "savetoexcelform" ).submit( );'><img src='./excel.png' width='48' height='48' border='0' title='Download to Excel' alt='Download to Excel' /></a>

<form action='convert.php' name='savetoexcelform' id='savetoexcelform' method='post' target='_blank' onsubmit='return excel( );'>
 <input type='hidden' id='dataToDisplay' name='dataToDisplay'>
 <input type='hidden' id='filename' name='filename' value='output.xls'>
</form>

谢谢。

按照惯例 javascript 假设验证已经在通过代码提交表单时执行。我建议您在调用 form.submit.

之前更改逻辑以格式化值

下面的代码可以提供帮助。

<a href='javascript:void(0);' onclick='submitForm()'>
<img src='./excel.png' width='48' height='48' border='0' title='Download to Excel' alt='Download to Excel' />
</a>

<form action='convert.php' name='savetoexcelform' id='savetoexcelform' method='post' target='_blank' onsubmit='return formatForm();'>
  <input type='hidden' id='dataToDisplay' name='dataToDisplay'>
  <input type='hidden' id='filename' name='filename' value='output.xls'>
</form>

<script type="text/javascript">

    function submitForm()
    {
        formatForm();
        document.getElementById( "savetoexcelform" ).submit( );
    }

    function formatForm()
    {
         document.getElementById( 'dataToDisplay' ).value = 
         document.getElementById( 'jfabtable' ).innerHTML;  // replaced, tested and works
}
</script>