当选定日期与日期时间选择器上的数据库日期匹配时,我想显示一个警报消息框。
尽管WebMethod运行良好,但我现在的代码是每次我选择的时候,总是发出警告消息而不进行检查。
Test.aspx.cs
[System.Web.Services.WebMethod]
public static string GetDateFromDB(DateTime compareDate)
{
string selectedDate = compareDate.ToString("yyyy/MM/dd");
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDBConnectionString1"].ConnectionString);
SqlCommand com = new SqlCommand("SELECT * from Holiday where Date='" + selectedDate + "'", conn);
SqlDataAdapter sqlDa = new SqlDataAdapter(com);
DataTable dt = new DataTable();
sqlDa.Fill(dt);
if (dt == null || dt.Rows.Count == 0)
return "NG";
else
return "OK";
}
Test.aspx
<link href="http://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<input type='text' class='date' id="datepicker" autocomplete="off">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
jQuery(function ($) {
$("#datepicker").datepicker({
onSelect: function (dateText) {
alert("Selected date: " + dateText + "; input's current value: " + this.value);
$(this).change();
$.ajax({
type: "POST",
url: "Test.aspx/GetDateFromDB",
data: '{ "compareDate" : "' + dateText + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
//success: OnSuccess,
//failure: function (response) {
// alert(response.d);
//}
});
},
}
).on("change", function () {
display("Got change event from field");
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
</script>
发布于 2019-04-08 08:22:58
它总是因为警报消息进入onChange事件而发出警报。在AJAX调用成功后,您需要将其移到。下面是你的编辑代码。
编辑:根据代码后面的返回值(即"OK“& "NG")检查条件。
<link href="http://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<input type='text' class='date' id="datepicker" autocomplete="off">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
jQuery(function ($) {
$("#datepicker").datepicker({
onSelect: function (dateText) {
$(this).change();
$.ajax({
type: "POST",
url: "Test.aspx/GetDateFromDB",
data: '{ "compareDate" : "' + dateText + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data)
{
if(data == "OK")
{
alert("Selected date: " + dateText + "; input's current value: " + this.value);
}
},
//failure: function (response) {
// alert(response.d);
//}
});
},
}
).on("change", function () {
display("Got change event from field");
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
</script>
注:以上更改未经测试.如果不起作用,请写评论。
https://stackoverflow.com/questions/55567472
复制相似问题