调用提交函数时出错(引用html表单)

时间:2017-11-28 20:58:38

标签: javascript jquery html

我已经查看了之前的问题,并且大多数人都说如果表单/输入名称是提交的,它将覆盖函数submit,但我没有任何名为submit的内容。我仍然收到此错误。

JS:

<div id="children" class="children module">
  <h3 class="heading">Works inspired by this one:</h3>
  <ul>
    <li>
        <a href="/works/240745">Cover-  Performance in A Leading Role by mad_lori</a> by <a rel="author" href="/users/blanketforyourshock/pseuds/blanketforyourshock">blanketforyourshock</a>
    </li>
    <li>
        <a href="/works/341326">Movie still: To A Stranger</a> by <a rel="author" href="/users/RedOrchid/pseuds/RedOrchid">RedOrchid</a>
    </li>
    <li>
        <a href="/works/787850">Future Yesterday</a> by <a rel="author" href="/users/ladymac111/pseuds/ladymac111">ladymac111</a>
    </li>
  </ul>
</div>

HTML:

<div id="reportrange" class="pull-left" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 25%">
<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>&nbsp;
<span></span> <b class="caret"></b> 

<script type="text/javascript">

$(function() {

    var start = moment().subtract(29, 'days');
    var end = moment();
    function cb(start, end) {
        $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

        document.getElementById('tests').value = start;
        document.getElementById('teste').value = end;
        document.getElementById('testing').submit();
    }

    $('#reportrange').daterangepicker({
        startDate: start,
        endDate: end,
        ranges: {
           'Today': [moment(), moment()],
           'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
           'Last 7 Days': [moment().subtract(6, 'days'), moment()],
           'Last 30 Days': [moment().subtract(29, 'days'), moment()],
           'This Month': [moment().startOf('month'), moment().endOf('month')],
           'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        }
    }, cb);

    cb(start, end);
});
</script>

1 个答案:

答案 0 :(得分:0)

你有一个好的开始。只需将函数连接到您的表单,如下所示:

<form  method="post" action="" onsubmit="return cb()">

这样,当您提交表单时,它将运行该功能。在您的函数中,您也可以通过返回false来取消提交,例如

function cb(start, end) {
     return false;
}

如果您没有返回false,或者您返回true,您将提交表单 - 无需提交()电话。

祝你好运!