Javascript 按下按钮验证多个字段

Javascript 按下按钮验证多个字段,javascript,html,asp.net-mvc,validation,Javascript,Html,Asp.net Mvc,Validation,我正在做一个MVC应用程序,我需要使用一个按钮来验证两个字段 目前,我有两个按钮和两个javascript函数,分别验证每个字段 功能1: <button onclick="callApi()" id="userButton">@Resources.Resources.ButtonFind</button> <div id="user"> <p>@Resources.Resources.UserName: </p> </div&g

我正在做一个MVC应用程序,我需要使用一个按钮来验证两个字段

目前,我有两个按钮和两个javascript函数,分别验证每个字段

功能1:

<button onclick="callApi()" id="userButton">@Resources.Resources.ButtonFind</button>
<div id="user">
<p>@Resources.Resources.UserName:
</p>
</div>
<script type="text/javascript"  id="select_user">
function callApi() {
    var userName = $('#search_employee').val();
    /* call the api */
    $.ajax('OneUserInfo', {
        data: {
            strUserName: userName
        },
        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... ");
        },
        dataType: "html",
        cache: false,   //important! especially for IE...
        async: false  //set to false if you want th UI to wait for the ajax call;
    });
}

function GO() {
    location.href = '/Home/NewspaperStatus?name=value';
}

</script>
@Resources.Resources.ButtonFind
@Resources.Resources.UserName:

函数callApi(){ var userName=$(“#搜索_员工”).val(); /*调用api*/ $.ajax('OneUserInfo'{ 数据:{ strUserName:用户名 }, 成功:函数(数据、文本状态、jqXHR){ //这将在请求成功时发生 $('#DataUser').html(数据); }, 错误:函数(){ log(“ajax请求失败时的错误处理程序…”); }, 数据类型:“html”, cache:false,//重要!特别是对于IE。。。 async:false//如果希望用户界面等待ajax调用,则设置为false; }); } 函数GO(){ location.href='/Home/NewspaperStatus?name=value'; }
功能2:

<div id="datePicker">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</div>
<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 () {
      intDate = Date;
      $("#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: {
                  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" id="dateButton">@Resources.Resources.ButtonFind</button>
<br>

@Resources.Resources.Date:

$(函数(){ intDate=日期; $(“#日期选择器”)。日期选择器({ //默认日期:“+1w”, 变化月:对, 变化年:是的, 月数:1, minDate:“01/01/2008” }); $(“button.action”)。单击(函数(){ //console.log(选择日期); var date=$('#datepicker').val().toString(); $.ajax('EmployeeDate'{ 数据:{ 日期 }, 成功:函数(数据、文本状态、jqXHR){ //这将在请求成功时发生 $('#DataUser').html(数据); }, 错误:函数(){ log(“ajax请求失败时的错误处理程序…”); }, }); }); }); @Resources.Resources.ButtonFind

如何编写代码,以便在同一个按钮上验证这两个字段

提前谢谢

编辑

谢谢你们。因此,如果我理解正确,我必须这样做:

 <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>

@Resources.Resources.Date:

$(函数getInfo(){ intDate=日期; var userName=$(“#搜索_员工”).val(); $(“#日期选择器”)。日期选择器({ //默认日期:“+1w”, 变化月:对, 变化年:是的, 月数:1, minDate:“01/01/2008” }); $(“button.action”)。单击(函数(){ //console.log(选择日期); var date=$('#datepicker').val().toString(); $.ajax('EmployeeDate'{ 数据:{ strUserName:用户名, 日期 }, 成功:函数(数据、文本状态、jqXHR){ //这将在请求成功时发生 $('#DataUser').html(数据); }, 错误:函数(){ log(“ajax请求失败时的错误处理程序…”); }, }); }); }); @Resources.Resources.ButtonFind

无论如何,我应该做些错事,因为我不能执行代码。“未定义函数”错误。代码中有不正确的地方吗?谢谢

我怎样才能将其更改为只有一个按钮的功能

可以通过在另一个函数中调用一个函数来实现这一点

function callApi() {
   $.ajax({
   success:function(response){
      //  Here you can call the second function 
    }
  })
}
我能把它改成只有一个功能吗

第二个函数有一个单独的ajax调用,您可以使用jquery将两个ajax调用链接到一个函数中。 此外,您还必须将与日期选择器相关的代码放在适当的位置,要么放在第一个ajax成功的一侧,要么放在执行任何ajax调用之前

我怎样才能将其更改为只有一个按钮的功能

可以通过在另一个函数中调用一个函数来实现这一点

function callApi() {
   $.ajax({
   success:function(response){
      //  Here you can call the second function 
    }
  })
}
我能把它改成只有一个功能吗

第二个函数有一个单独的ajax调用,您可以使用jquery将两个ajax调用链接到一个函数中。
此外,您还必须将与日期选择器相关的代码放在适当的位置,无论是在第一个ajax成功的情况下,还是在执行任何ajax调用之前,都有几种方法可以做到这一点。例如,您可以合并/扩展您的数据属性,并从服务器返回无效的属性,然后在ajax中处理该属性。但是,如果您不能/不想合并,也可以使用Jquery promise功能。这样,您就可以更好地控制上一次呼叫成功/失败等情况下的下一步操作

有几种方法可以做到这一点。例如,您可以合并/扩展您的数据属性,并从服务器返回无效的属性,然后在ajax中处理该属性。但是,如果您不能/不想合并,也可以使用Jquery promise功能。这样,您就可以更好地控制上一次呼叫成功/失败等情况下的下一步操作

这两个函数更改元素“#DataUser”。。。这个未来的独特功能对成功有什么作用?它应该是两种价值观的结合。现在,两个separtley都改变了dataUser,但是如果只有一个按钮,那么只有这个按钮应该改变它。好吧,我只会做一个ajax调用,改变接受请求的控制器。。。否则,正如您在回答中所看到的,您可以使用“承诺”进行两次ajax调用,并在第二次调用之后打印数据。。。这个未来的独特功能对成功有什么作用?它应该是两种价值观的结合。现在,两个separtley都改变了dataUser,但是如果只有一个按钮,那么只有这个按钮应该改变它。好吧,我只会做一个ajax调用,改变接受请求的控制器。。。否则,正如你在回答中所看到的,你可以用“承诺”来表达你的意思