执行代码时出现Javascript运行时错误

时间:2016-07-14 07:17:35

标签: javascript html asp.net asp.net-mvc

我希望从表单中获取两个值,并在按下按钮时将其传递给控制器​​。

我到目前为止的代码是:

  <div id="date">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <p>@Resources.Resources.Date: <input type="text" id="datepicker"></p>
  <script name="select_date" id="select_date">

  $(function getInfo() {
      intDate = Date;
      var userName = $('#search_employee').val();
      $("#datepicker").datepicker({
          //defaultDate: "+1w",
          changeMonth: true,
          changeYear: true,
          numberOfMonths: 1,
          minDate: "01/01/2008"
      });
      $("button.action").click(function () {
          //console.log(select_date);
          var date = $('#datepicker').val().toString();
          $.ajax('EmployeeDate', {
              data: {
                  strUserName: userName,
                  strDate: date
              },
              success: function (data, textStatus, jqXHR) {
                  //this will happen on success of request
                  $('#DataUser').html(data);
              },
              error: function () {
                  console.log("error handler when ajax request fails... ");
              },

          });
      });
  });
</script>
<button onclick="getInfo()" id="userButton">@Resources.Resources.ButtonFind</button>
<br>

`

当我执行代码时,我收到此错误:

  

0x800a1391 - JavaScript运行时错误:&#39; getInfo&#39;未定义

代码中有什么问题?

谢谢!

修改

 $(function () {
      intDate = Date;
      var userName = $('#search_employee').val();
      $("#datepicker").datepicker({
          //defaultDate: "+1w",
          changeMonth: true,
          changeYear: true,
          numberOfMonths: 1,
          minDate: "01/01/2008"
      });
      $("button.action").click(function () {
          //console.log(select_date);
          var date = $('#datepicker').val().toString();
          $.ajax('EmployeeDate', {
              data: {
                  strUserName: userName,
                  strDate: date
              },
              success: function (data, textStatus, jqXHR) {
                  //this will happen on success of request
                  $('#DataUser').html(data);
              },
              error: function () {
                  console.log("error handler when ajax request fails... ");
              },

          });
      });
  });
</script>
    <button class="action"    type="button">@Resources.Resources.ButtonFind</button>
<br>

当我按下按钮时没有任何反应..谢谢

2 个答案:

答案 0 :(得分:1)

尝试添加不带$()的函数;

像这样

function getInfo() {
  intDate = Date;
  var userName = $('#search_employee').val();
  $("#datepicker").datepicker({
      //defaultDate: "+1w",
      changeMonth: true,
      changeYear: true,
      numberOfMonths: 1,
      minDate: "01/01/2008"
  });
  $("button.action").click(function () {
      //console.log(select_date);
      var date = $('#datepicker').val().toString();
      $.ajax('EmployeeDate', {
          data: {
              strUserName: userName,
              strDate: date
          },
          success: function (data, textStatus, jqXHR) {
              //this will happen on success of request
              $('#DataUser').html(data);
          },
          error: function () {
              console.log("error handler when ajax request fails... ");
          },

      });
  });
}

答案 1 :(得分:1)

这是一个范围问题。 您有两种选择:

1.将函数移动到$(document).ready()

之外的全局范围内
<script>
    function getInfo() {
       ...
    };
</script>
<button onclick="getInfo()" id="userButton">@Resources.Resources.ButtonFind</button>

2.管理$(文档).ready()

中的单击侦听器
$(function() {

    function getInfo() {
        ...
    };


    $('button#userButton').click(function(e){
        getInfo();
    })

  });
<button id="userButton">@Resources.Resources.ButtonFind</button>