[英]Javascript runtime error when execute the code
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 0x800a1391-JavaScript运行时错误:'getInfo'未定义
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 当我按下按钮时,什么都没有发生..谢谢
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... ");
},
});
});
}
It's a matter of scope. 这是范围的问题。 You have two options: 您有两种选择:
1.Move the function in the global scope outside the $(document).ready() 1.将函数移到$(document).ready()之外的全局范围内
<script>
function getInfo() {
...
};
</script>
<button onclick="getInfo()" id="userButton">@Resources.Resources.ButtonFind</button>
2.Manage the click listener inside the $(document).ready() 2.管理$(document).ready()中的点击侦听器
$(function() {
function getInfo() {
...
};
$('button#userButton').click(function(e){
getInfo();
})
});
<button id="userButton">@Resources.Resources.ButtonFind</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.