如何在重载页面后在AJAX success函数中设置值?

时间:2021-04-04 19:44:41

How to set a value to Textarea control in AJAX success function after reloading the page?

如何在重载页面后,在AJAX success函数中设置Textarea控件的值?

by below code I am getting a value in page view i.e, "Create.cshtml"

通过下面的代码,我在page view I中获得一个值。e:“Create.cshtml”

<script>
  $(document).ready(function () {
    $(".setLang").on("click", function (event) {
      var txt = $("#About").val();
      var langge = $(this).attr("data-lang");

      if (txt != null && txt != "" && langge != null && langge != "") {
        $.ajax({
          type : "POST",
          url :  "@Url.Action("Translation", "Home")",
          traditional: true,
          data: { text: txt, language: langge },
          success: successFunc,
          error: errorFunc
        });

        function successFunc(data){
          alert(data);
          $("#About").val(data);
        }
      }
    });
  });
</script>

but when I am trying to set it like below, its cant taking in 'textarea'

但是当我试图设置它时,它不能接收textarea

function successFunc(data) {
  alert(data);
  $("#About").val(data);
}

mean while in shared view some code running like below i,e, "_Layout.cshtml"

在共享视图中运行的一些代码,比如下面的ie,“_Layout.cshtml”

<script>
  $(document).ready(function () {
    var lang = MultiLanguageDemo.Cookies.getCookie("LangForMultiLanguageDemo");
    $(".setLang[data-lang='" + lang + "'] img").addClass("active-lang");
    $(".setLang").on("click", function (event) {
      var lang = $(this).attr("data-lang");                  
      MultiLanguageDemo.Cookies.setCookie("LangForMultiLanguageDemo", lang, 30);    
      location.reload(true);
    })
  });
</script>

the text area is

文本区域是

<div class="form-group">
   @Html.Label("About", new { @class = "control-label col-md-2" })
   <div class="col-md-10">
     @Html.TextAreaFor(model => model.About, new { htmlAttributes = new { @class = "form-control" } })
     @Html.ValidationMessageFor(model => model.About, null, new { @class = "text-danger" })
   </div>
</div>

2 个解决方案

#1


0  

Here you go with a solution

给你一个解

$(document).ready(function () {

 if(localStorage.getItem("textareamessage") != null){
   $("#About").val(localStorage.getItem("textareamessage"));
 }

  $(".setLang").on("click", function (event) {
    var txt = $("#About").val();
    var langge = $(this).attr("data-lang");

    if (txt != null && txt != "" && langge != null && langge != "") {
      $.ajax({
        type : "POST",
        url :  "@Url.Action("Translation", "Home")",
        traditional: true,
        data: { text: txt, language: langge },
        success: successFunc,
        error: errorFunc
      });

      function successFunc(data){
        alert(data);
        localStorage.setItem("textareamessage", data);
        $("#About").val(data);
      }
    }
  });
});

I've used localStorage instead of cookies.

我使用了localStorage而不是cookies。

Please share the response data of AJAX success.

请分享AJAX成功的响应数据。

Hope this will help you.

希望这能对你有所帮助。

#2


0  

Have you tried using (if you use html)

你试过使用(如果你使用html)吗?

$("#About").html(data);

or

$("#About").append(data);

Also have you checked the data is not null? Because what I see you call the function without passing any data.

你也检查过数据不为空吗?因为我看到你不用传递任何数据就能调用函数。

Maybe doing like this:

也许这样做:

success: function(data) {
  $("#About").html(data);
}

Or

success: succesFunc(data)

Sven

斯文

#1


0  

Here you go with a solution

给你一个解

$(document).ready(function () {

 if(localStorage.getItem("textareamessage") != null){
   $("#About").val(localStorage.getItem("textareamessage"));
 }

  $(".setLang").on("click", function (event) {
    var txt = $("#About").val();
    var langge = $(this).attr("data-lang");

    if (txt != null && txt != "" && langge != null && langge != "") {
      $.ajax({
        type : "POST",
        url :  "@Url.Action("Translation", "Home")",
        traditional: true,
        data: { text: txt, language: langge },
        success: successFunc,
        error: errorFunc
      });

      function successFunc(data){
        alert(data);
        localStorage.setItem("textareamessage", data);
        $("#About").val(data);
      }
    }
  });
});

I've used localStorage instead of cookies.

我使用了localStorage而不是cookies。

Please share the response data of AJAX success.

请分享AJAX成功的响应数据。

Hope this will help you.

希望这能对你有所帮助。

#2


0  

Have you tried using (if you use html)

你试过使用(如果你使用html)吗?

$("#About").html(data);

or

$("#About").append(data);

Also have you checked the data is not null? Because what I see you call the function without passing any data.

你也检查过数据不为空吗?因为我看到你不用传递任何数据就能调用函数。

Maybe doing like this:

也许这样做:

success: function(data) {
  $("#About").html(data);
}

Or

success: succesFunc(data)

Sven

斯文