Reputation: 3271
I want to get two values from a form and pass it to the controller while I press a button.
The code I have so far is:
<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>
`
When I execute the code I get this error:
0x800a1391 - JavaScript runtime error: 'getInfo' is undefined
Whats wrond in the code?
Thanks!
EDIT
$(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>
when I press the button nothing happens.. thanks
Upvotes: 0
Views: 874
Reputation: 181
It's a matter of scope. You have two options:
1.Move the function in the global scope outside the $(document).ready()
<script>
function getInfo() {
...
};
</script>
<button onclick="getInfo()" id="userButton">@Resources.Resources.ButtonFind</button>
2.Manage the click listener inside the $(document).ready()
$(function() {
function getInfo() {
...
};
$('button#userButton').click(function(e){
getInfo();
})
});
<button id="userButton">@Resources.Resources.ButtonFind</button>
Upvotes: 1
Reputation: 128
try to add the function without $();
like this
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... ");
},
});
});
}
Upvotes: 1