将值从一个js函数发送到另一个作为参数 [英] sending value from one js function to another as parameter

查看:127
本文介绍了将值从一个js函数发送到另一个作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript新手问题 我正在从文本中获取用户输入,并使用该值发送给控制器以进行进一步处理.在页面初始化上,一切都很好,现在我想绑定确定"按钮,以将用户值发送到我的页面初始化脚本中(我试图避免复制脚本).这是代码

Javascript newbie question I'm fetching user input from text and use that value to send to the controller for further process. On page init everything is fine, now I want to bind OK button to send users value to my page init script (I'm trying to avoid copying script). Here's the code

@Html.ActionLink("OK", "Ajax", null, new { @class = "button", @id ="myDate" }) 

页面初始化

$(document).ready(function dataTable() {
  $('#dataTable').dataTable({
            "bServerSide": true,
             "fnServerParams": function (aoData) {
                var date = $('input[name="myDate"]').val();
                aoData.push({ "name": "Date", "value": date });
   });
});

在用户输入上,然后单击按钮,我应该接受该输入并将其发送到上述脚本进行处理

on user input and clicking the button I should take that input and sent to the above script to process

 $('#myDate').click(function () { 
     var date = $('input[name="myDate"]').val();
     // ????
     // Should I change first function to receive parameter as argument 

});

推荐答案

一种方法是分解代码以使日期超出数据表init

One way is to factor out the code to get the date outside the datatable init

function getDate(){
    var date = $('input[name="myDate"]').val();
    return date;
}

然后在数据表init中

Then in your datatable init

 var date = getDate();

与您的点击事件相同

 $('#myDate').click(function () { 
     var date = getDate();
  });

您应该以此结束

    $(document).ready(function dataTable() {
        function getDate(){
        var date = $('input[name="myDate"]').val();
       return date;
      }

     $('#dataTable').dataTable({
        "bServerSide": true,
         "fnServerParams": function (aoData) {
            var date = getDate();
            aoData.push({ "name": "Date", "value": date });
               });
           });


              $('#myDate').click(function () { 
                 var date = getDate();
              });
        });

这篇关于将值从一个js函数发送到另一个作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆