【问题标题】:JQuery Values are not changingJQuery 值没有改变
【发布时间】:2015-08-07 10:28:42
【问题描述】:
 @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。一切运行顺利。但我有一个奇怪的问题,即当我第一次单击按钮时,它从文本框中获取值并相应更改,但是当我再次按下按钮时,它不会从文本框中获取新值,而是它以某种方式使用我输入的旧值第一次!

【问题讨论】:

标签: javascript jquery asp.net-mvc asp.net-mvc-4 html.actionlink


【解决方案1】:

因为在第一次单击后,您将替换 href 中的 ssseee,因此之后的 href 中没有 ssseee。所以第一次点击后什么都不会被替换

因此,一种可能的解决方案是将原始 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);
});

【讨论】:

  • 好的,我明白了!但我想如何存储“sss”、“eee”??
  • 对不起..store "sss" , "eee"是什么意思
【解决方案2】:

基本上,在您的 jQuery 代码中,您可以通过替换 ssseee 创建一个新链接,但是一旦替换了它们,就再也找不到它们了

this.href = this.href.replace("sss", StartDate); // sss no longer exists after this
this.href = this.href.replace("eee", EndDate); // sss no longer exists after this

您需要做的是在修改之前存储原始 href 值,然后在要更新链接时引用它

$("#btnDateWiseGuestSearch").bind('click', function () {
    var $this = $(this);
    var originalhref = $this.data("href");
    if(!originalhref){
        this.data("href", this.href);
    }

    var EndDate = $("#txtDateWiseGuestEndDate").val();
    var StartDate = $("#txtDateWiseGuestStartDate").val();

    this.href = originalhref.replace("sss", StartDate).replace("eee", EndDate);
 });

【讨论】:

    猜你喜欢
    • 2013-01-12
    • 1970-01-01
    • 2014-12-02
    • 2010-10-08
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    相关资源
    最近更新 更多