将值从一个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();
});
});