2013-07-13 39 views
1

我正在使用涉及表單的jQuery手機構建網站。使用Ajax和PHP更新表單中的JQuery Mobile選擇菜單

我一直在努力實現這個方法來動態改變選擇菜單: Link

雖然我已經能夠做到這一點我的jQuery Mobile的網站之外,它似乎並沒有使用jQuery移動攜手合作。我已經設置了data-ajax="false"並嘗試了一堆東西,但我找不到解決方案。這是因爲jQuery移動或不?

您可以在下面找到該代碼。

對於主頁:

<?php require('config_db.php');?> 

... 

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" /> 
<link rel="stylesheet" type="text/css" href="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.min.css" /> 
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script> 

... 

<div data-role="fieldcontain"> 
    <label for="course">Course:</label> 
    <select id="course" name="course" data-native-menu="false"> 
     <option value="0">0</option> 
     <option value="1">1</option> 
    </select> 
</div> 
<div data-role="fieldcontain"> 
    <label for="student">Student:</label> 
    <select id="student" name="Student" data-native-menu="false"> 
    </select> 
</div> 

... 

<script type="text/javascript"> 
$(function() { 
$("#course").bind("change", function() { 
    $.ajax({ 
     type: "GET", 
     url: "get_students.php", 
     data: "course="+$("#course").val(), 
     success: function(html) { 
      $("#student").html(html); 
     } 
    }); 
}); 
}); 
</script> 

Config_db.php已經測試和工程就像一個魅力。

Get_students.php工程,以及和將產生從測試數據庫以下的輸出:

<option value='1'>John Doe</option><option value='2'>Jane Doe</option> 

我曾做過一個試驗,併除去開頭的jQuery Mobile的線解決了這個問題。不幸的是,我確實需要jQuery手機。

+0

您必須提供更多信息。你的代碼特別有用。 – SharkofMirkwood

+0

謝謝!我已經添加了一些代碼。 – MacBryce

+0

有趣的是,它必須是JQuery移動本身正在做的事情。您是否檢查過Javascript控制檯以查看它是否報告了任何內容? – SharkofMirkwood

回答

5

JQuery手機不只是重新選擇一個選擇框,它增加了它自己的複雜標記。你需要告訴jQuery Mobile來更新它(移動顯示)。

<script type="text/javascript"> 
$(function() { 
$("#course").bind("change", function() { 
    $.ajax({ 
     type: "GET", 
     url: "get_students.php", 
     data: "course="+$("#course").val(), 
     success: function(html) { 
      $("#student").html(html).selectmenu('refresh', true); 
     } 
    }); 
}); 
}); 
</script> 
+0

爲什麼不選這個答案? – alkis

+0

這應該可行,但不要在jQuery Mobile中使用'.ready'。 – Omar

+0

非常感謝。這解決了這個問題,它讓我更好地理解jQuery mobile的實際工作原理。 :-) – MacBryce

相關問題