gpt4 book ai didi

javascript - 单击 a href 时 Jquery Ajax 调用将数据发送到方法

转载 作者:行者123 更新时间:2023-11-28 06:08:20 26 4
gpt4 key购买 nike

我正在尝试将从我的母版页单击的语言类型发送到 App_code 文件夹中的 surfacecontroller。我的 HTML 如下所示:

<div class="navbar-topbar clearfix">
<div class="h5 pull-right">
<span class="linkCA">
<strong>CA</strong>
<a class="lang=en-CA" href="@NewUrlLink">EN</a> |
<a class="lang=fr-CA" href="@NewUrlLink">FR</a>
</span>
<span class="linkUS">
<strong>US</strong>
<a class="lang=en-US" href="@NewUrlLink">EN</a>
</span>
</div>
</div>

所以我的想法是获取类名,并在单击 a 时通过 AJAX 调用发送它。我有这个方法,我想将单击的语言类型传递给:

public string GetDictionaryItemByCulture(string key, string language)
{
var currentLang = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
var otherLang = myCulture(language);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(otherLang);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(otherLang);

// String noLastSegment = Request.Url.AbsolutePath;
string x = new UmbracoHelper().GetDictionaryValue(key);

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(currentLang);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(currentLang);

return x;
}

我在网上看到了这个 AJAX 方法,它看起来像我需要的,但我不知道如何连接它来满足我的需要。

$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: { someParameter: "some value" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});

最佳答案

首先,最好将语言环境值放置在 HTML 中的 data-* 属性中,每个 a 都给定一个事件处理程序可以使用的公共(public)类附于:

<div class="navbar-topbar clearfix">
<div class="h5 pull-right">
<span class="linkCA">
<strong>CA</strong>
<a data-lang="en-CA" class="lang-trigger" href="@NewUrlLink">EN</a> |
<a data-lang="fr-CA" class="lang-trigger" href="@NewUrlLink">FR</a>
</span>
<span class="linkUS">
<strong>US</strong>
<a data-lang="en-US" class="lang-trigger" href="@NewUrlLink">EN</a>
</span>
</div>
</div>

从那里您可以点击a,读取data-lang属性并发送信息:

$('.lang-trigger').click(function() {
var $a = $(this);
$.ajax({
type: "POST",
url: "GetDictionaryItemByCulture/",
data: {
key: 'whatever this value should be...',
lang: $a.data('lang')
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
console.log(msg);
}
});
});

关于javascript - 单击 a href 时 Jquery Ajax 调用将数据发送到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36626677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com