【发布时间】:2019-03-07 10:10:32
【问题描述】:
我想在 jquery 中对每个做一个 foreach。
我的函数 jquery 调用了另一个函数 showTauxPlafonds,所以我希望每次调用这个函数都可以使用 ajax 返回结果。
对于我想要的每个<a>,当我单击时,我都会调用我的 function jquery:
$("a").each(function() {
$(this).on("click", function() {
showTauxPlafonds(urlTaux, $(this).attr('tauxPlafonds'));
})
});
var showTauxPlafonds = function(url, tauxPlafonds) {
if (tauxPlafonds != null)
getAjaxedHtmlView(url, {
tauxPlafonds: tauxPlafonds
}, null, 'app_taux_plafonds_values');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="mdc-list-item" tauxPlafonds="PSS" data-toggle="tooltip" data-placement="top" title="Acceuil" style="cursor:pointer;width:350px">
Plafond sécurité sociale
</a>
<a class="mdc-list-item" data-toggle="tooltip" data-placement="top" title="Taux et plafonds" style="cursor:pointer;width:350px">
Smic
</a>
【问题讨论】:
-
您可以直接绑定到锚点,不需要每个 -
$('a').on("click", function() {尽管您的代码应该可以工作 - 您的确切问题是什么?是不是链接正在加载一个新页面,所以看起来你的代码没有被命中?或者$(this)不是showTauxPlafonds(urlTaux, $(this).attr('tauxPlafonds'));中的样子 -
我给你做了一个合适的sn-p。请至少添加一个虚拟
getAjaxedHtmlView -
这一行 -
showTauxPlafonds(urlTaux, $(this).attr('tauxPlafonds'));-urlTaux定义在哪里? -
@Pete 我是怎么做到的?
-
你能做什么?你还没说你的问题是什么?
标签: javascript jquery html