javascript - 收到错误 Uncaught TypeError : Cannot set property 'value' of null

标签 javascript html

我收到此错误Uncaught TypeError: Cannot set property 'value' of null, on ->

我的 html 是从函数 goSubmit() 传递值,

<form class="form-inline" id="formdaily" action="http://localhost:8080/reports/storewisedaily" method="POST">
          <div class="box">
            <div class="box-header">
              <h3 class="box-title">Total Paid Orders - Report Data</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="datatables" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>Month - Year</th>
                  <th>Amount</th>
                </tr>
                </thead>
                <tbody>

                                      <tr>
                      <td><a id="month_year" data-id="2019-01" onclick="goSubmit('2019-01')" href="#">2019-01</a></td>
                      <td>0.00</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-02" onclick="goSubmit('2019-02')" href="#">2019-02</a></td>
                      <td>0.00</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-03" onclick="goSubmit('2019-03')" href="#">2019-03</a></td>
                      <td>0.00</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-04" onclick="goSubmit('2019-04')" href="#">2019-04</a></td>
                      <td>0.00</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-05" onclick="goSubmit('2019-05')" href="#">2019-05</a></td>
                      <td>11,471.50</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-06" onclick="goSubmit('2019-06')" href="#">2019-06</a></td>
                      <td>0.00</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-07" onclick="goSubmit('2019-07')" href="#">2019-07</a></td>
                      <td>12.50</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-08" onclick="goSubmit('2019-08')" href="#">2019-08</a></td>
                      <td>0.00</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-09" onclick="goSubmit('2019-09')" href="#">2019-09</a></td>
                      <td>0.00</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-10" onclick="goSubmit('2019-10')" href="#">2019-10</a></td>
                      <td>0.00</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-11" onclick="goSubmit('2019-11')" href="#">2019-11</a></td>
                      <td>0.00</td>
                    </tr>
                                      <tr>
                      <td><a id="month_year" data-id="2019-12" onclick="goSubmit('2019-12')" href="#">2019-12</a></td>
                      <td>0.00</td>
                    </tr>
                                    <input type="hidden" id="select_month" name="select_month" value="">
                  <input type="hidden" id="select_store" name="select_store" value="3">
                </tbody>
                <tfoot align="right">
                  <tr><th></th><th></th></tr>
                </tfoot>
              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
          </form>

在函数中,我想将 goSubmit() 的值传递给名为“select_month”的隐藏输入

  document.getElementById('select_month').value = m;

即使我对值进行硬编码,它似乎也没有通过。有人可以帮我周五和周六吗

<input type="hidden" id="select_month" name="select_month" value="">

function goSubmit(month_year) {
        alert(month_year); // it pass value '2019-05'
        var m = month_year.toString();
        console.log(m);
        alert(m);
        //alert($('#select_month').val(m));
        document.getElementById('select_month').value = m;
        //alert(document.getElementById("select_month").value);
        //alert($('#select_month').val(month_year));
        //$("#select_store").val(3);
        document.getElementById('select_store').value = 3;
        document.getElementById('formdaily').submit();
      }

最佳答案

Value 是存在于输入标签上的属性,所以请尝试一下。 document.getElementById("select_month").value = m

关于javascript - 收到错误 Uncaught TypeError : Cannot set property 'value' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57319866/

相关文章:

javascript - 在html5中单击按钮/链接时如何将一个页面导航到另一个页面

c# - WebMethod 返回 JSON 对象问题

php - 如何创建布局模板 - 静态网站

html - 应用显示 :block to table cell td 时表格布局不正确

javascript - AWS LexRuntime API-发送音频

javascript - javascript声明变量必须要用关键字 'var'吗

javascript - 如何滑动响应式 SVG slider ?

css - 具有可变高度 iframe 和 div 框的 div 容器无法正确扩展

html - 如何将固定位置元素放在相对定位元素之上?

javascript - 允许用户在 Bootstrap 模式之外进行交互