我的Rails应用加载包含链接的部分,以及加载的脚本,为每个链接创建onClick
个事件。
当正常加载页面时(不是通过AJAX),这些链接呈现正常。
然而,当我通过AJAX加载部分时,那些onClick事件并没有被触发。
当我对load_links_url
进行ajax调用时,其视图包含以下内容:
$("#my_div").html("#{j render('links/links', the_links: @links)}");
onClick事件未触发。那是为什么?
部分links/_links.html.haml
如下所示:
-links.each do |link|
=link_to link.name, "javascript:;", id: "select_link_#{link.id}", class: "select-link", "data-id" => link.id, "data-name" => link.name
:javascript
$(".select-link").click(function() {
var id = $(this).data("id");
var thename = $(this).data("name");
$('#link_name_header').html(thename);
$('#creative_session_link_id').val(id);
})
答案 0 :(得分:0)
查看Jquery event handler not working on dynamic content
总之,对于动态添加的内容,请使用.on和适当的选择器进行事件委派。就像你的情况一样:
Route::group(['before' => 'auth','prefix' => 'admin','middleware' => 'auth'], function($router) {
Route::get('/advertisement', [
'as' => 'get_advertisement_details',
'uses' => 'AdvertisementController@getIndex',
]);
});
Route::group(['before' => 'auth','prefix' => 'customer','middleware' => 'auth'], function($router) {
Route::get('/advertisement', [
'as' => 'get_advertisement_details',
'uses' => 'AdvertisementController@getIndex',
]);
});
Route::group(['before' => 'auth','prefix' => 'agent','middleware' => 'auth'], function($router) {
Route::get('/advertisement', [
'as' => 'get_advertisement_details',
'uses' => 'AdvertisementController@getIndex',
]);
});
And i also changed the authenticate.php as below
if(Auth::user()->hasRole(ltrim($request->route()->getPrefix(),'/')))
return $next($request);
else
return response('Unauthorized.', 401);
Finally i get all links(route) by agent prefix.