将动态变量发送到jQuery [英] Sending dynamic variable to jquery
本文介绍了将动态变量发送到jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用EJS模板动态构建的表.我想在用户单击按钮时将动态值传递给jQuery函数.
I have a table that is built dynamically with EJS template. I want to pass a dynamic value to a jQuery function when the user clicks on the button.
这是我建立桌子的方式:
This is how I'm building my table:
<% for(var i=0; i< automated_campaigns.length; i++) { %>
...
<%= automated_campaigns[i].name %>
<td>
<button type="button" class="btn btn-danger btn" id="load" data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i>">Activate</button>
</td>
...
<% } %>
这是我的jQuery函数,用于处理click事件:
And this is my jQuery function that handles the click event:
<script type="text/javascript">
$('.btn').on('click', function(automated_campagin_name) {
var $this = $(this);
$this.button('loading');
$.get( "/automated-campaigns/change_status/" + str(automated_campaign_name), function( data ) {
$( ".result" ).html( data );
alert( "Load was performed." );
});
setTimeout(function() {
$this.button('reset');
}, 8000);
});
</script>
如何发送正确的campaign_name?
How can I send the right campaign_name?
推荐答案
将campaign_name
放在data-campaign_name
中,因为您已经为每个循环了,然后在jQuery中访问它.
Put the campaign_name
in a data-campaign_name
since you are already looping for each of them then access it in jQuery.
$('.someElement').data('campaign_name');
这篇关于将动态变量发送到jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文