按下按钮时调用函数
Call a function when I press a button
我是 Java 脚本的新手,所以这个问题可能有点基础。我在 asp.net 上开发了一个网络应用程序,并且我有一个日历。所以目前每次我选择一个日期,它都会自动转到控制器和模型。
问题是我想在单击按钮而不是单击日期时执行事件。到目前为止,这是我的代码:
<div id="datePicker">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</div>
<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 selectDate() {
intDate = Date;
$("#datepicker").datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
minDate: "01/01/2008",
onClose: function (select_date) {
//console.log(select_date);
var date = $('#datepicker').val().toString();
$.ajax('NewspaperDate', {
data: {
strDate: date
},
success: function (data, textStatus, jqXHR) {
//this will happen on success of request
$('#DataNewspaper').html(data);
// window.location = "NewspaperDate?strDate=" + date;
},
error: function () {
console.log("error handler when ajax request fails... ");
},
});
}
});
});
我知道我需要更改 onClose
并通过按钮调用函数,但我无法使其工作。
没什么大问题。只需创建一个按钮并为其添加一个 click
事件侦听器。
<button class="action" type="button">send</button>
您可以使用 jQuery
,您在页面中实际包含的内容:
$(function() {
// init datepicker
$("#datepicker").datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
minDate: "01/01/2008"
});
// create a button event
$("button.action").click(function() {
$.ajax("NewspaperDate", {
data: {
strDate: $("#datepicker").val().toString()
},
success: function(response) {
$("#DataNewspaper").html(response);
}
});
});
});
提示:您应该将代码包装在 jQuery 就绪状态,就像我上面的示例一样。
$(function() {
// do your work, e.g use plugins or create event listener ...
});
我是 Java 脚本的新手,所以这个问题可能有点基础。我在 asp.net 上开发了一个网络应用程序,并且我有一个日历。所以目前每次我选择一个日期,它都会自动转到控制器和模型。
问题是我想在单击按钮而不是单击日期时执行事件。到目前为止,这是我的代码:
<div id="datePicker">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</div>
<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 selectDate() {
intDate = Date;
$("#datepicker").datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
minDate: "01/01/2008",
onClose: function (select_date) {
//console.log(select_date);
var date = $('#datepicker').val().toString();
$.ajax('NewspaperDate', {
data: {
strDate: date
},
success: function (data, textStatus, jqXHR) {
//this will happen on success of request
$('#DataNewspaper').html(data);
// window.location = "NewspaperDate?strDate=" + date;
},
error: function () {
console.log("error handler when ajax request fails... ");
},
});
}
});
});
我知道我需要更改 onClose
并通过按钮调用函数,但我无法使其工作。
没什么大问题。只需创建一个按钮并为其添加一个 click
事件侦听器。
<button class="action" type="button">send</button>
您可以使用 jQuery
,您在页面中实际包含的内容:
$(function() {
// init datepicker
$("#datepicker").datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
minDate: "01/01/2008"
});
// create a button event
$("button.action").click(function() {
$.ajax("NewspaperDate", {
data: {
strDate: $("#datepicker").val().toString()
},
success: function(response) {
$("#DataNewspaper").html(response);
}
});
});
});
提示:您应该将代码包装在 jQuery 就绪状态,就像我上面的示例一样。
$(function() {
// do your work, e.g use plugins or create event listener ...
});