2017-09-20 145 views
0

我正在創建一個div,我可以根據選擇值填充不同的內容。它起作用,當我選擇一個選項時,它會加載。我不知道該怎麼做是在第一次加載頁面時加載第一個選項。我也遇到了一個問題,在我的頭文件中調用的jQuery腳本在加載時不適用於ajax內容。我希望通過在頁面加載時加載第一個實例來解決這個問題。加載頁面加載時首先選擇選項值與ajax

這裏是我的代碼:

<select class="post-link"> 
<option value="http://example.com">Option 1</option> 
<option value="http://example.com">Option 2</option> 
<option value="http://example.com">Option 3</option> 
</select> 

// This is where the content will be loaded 
<div id="single-post-container"></div> 

jQuery(document).ready(function($){ 
    $.ajaxSetup({cache:false}); 
    $(".post-link").change(function(){ 
     var post_link = $(this).val(); 

     $("#single-post-container").html("content loading"); 
     $("#single-post-container").load(post_link); 
    return false; 
    }); 
}); 

任何方向將不勝感激!

+0

你想加載什麼?您是否嘗試根據您選擇的狀態加載內容? –

+0

這是我正在做的簡化版本。我從下拉列表中選擇帖子標題時,使用永久鏈接加載wordpress single.php文件。 – RiotAct

回答

1
jQuery(document).ready(function($){ 
    $.ajaxSetup({cache:false}); 
    $(".post-link").change(function(){ 
     var post_link = $(this).val(); 

     $("#single-post-container").html("content loading"); 
     $("#single-post-container").load(post_link); 
    return false; 
    }); 

    # selects first element 
    $(".post-link")[0].selectedIndex = 0; 

    # triggers change handler 
    $(".post-link").trigger('change'); 
}); 
+0

這個工程!我仍然有一個jQuery問題,不影響ajax加載的內容。這可能必須是一個不同的線程。 – RiotAct

+0

選中此:https://stackoverflow.com/questions/889967/jquery-load-call-doesnt-execute-javascript-in-loaded-html-file –

相關問題