@Html.ActionLink("Search", "GetDateWiseGuestReport", "Reports", new { StartDate = "sss",EndDate="eee" }, new { @id = "btnDateWiseGuestSearch", @class = "btn btn-red" })
$("#btnDateWiseGuestSearch").bind('click', function () {
//Get the id of the selected item in dropdown
var EndDate = $("#txtDateWiseGuestEndDate").val();
var StartDate = $("#txtDateWiseGuestStartDate").val();
this.href = this.href.replace("sss", StartDate);
this.href = this.href.replace("eee", EndDate);
});
好的,我正在使用上面的代码在运行时更改操作链接 URL。一切都运行顺利。但我有一个奇怪的问题,即当我第一次单击按钮时,它从文本框中获取值并相应地更改,但是当我再次按下按钮时,它不会从文本框中获取新值,而是以某种方式使用我输入的旧值第 1 次!
最佳答案
因为在第一次点击后,您将替换 href 中的 sss
和 eee
,所以之后没有 sss
或 eee
在 href 中。所以第一次点击后什么都没有被替换
因此,一个可能的解决方案是将原始 href 值存储在其他地方,然后使用它来替换内容。在下面的解决方案中,数据 api 用于存储原始值
var $btn = $("#btnDateWiseGuestSearch");
$btn.data('href', $btn.attr('href'))
$btn.bind('click', function () {
//Get the id of the selected item in dropdown
var EndDate = $("#txtDateWiseGuestEndDate").val();
var StartDate = $("#txtDateWiseGuestStartDate").val();
var href = $(this).data('href');
this.href = href.replace("sss", StartDate).replace("eee", EndDate);
});
关于javascript - JQuery 值没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31875544/