2010-11-15 129 views
1

我對JQuery非常陌生,並且根本沒有太多有關JavaScript的經驗,並且經過幾個小時的努力才發現我放棄了。我知道如何在PHP中做到這一點,但是當我在JQuery中嘗試時,我錯過了一些東西。如何將HTML下拉框中的值發送到JQuery函數

我有一個html文件生成一個dorpdown盒子,看起來像tihs:

<form action="" method="GET"> 
    <select id="countiesList" name="counties"> 
    <option value="all" selected="selected">All Counties</option> 
    <option value="county1">County 1</option> 
    <option value="county2">County 2</option> 
    ... 
    </select> 
</from> 
  1. 我如何從dropdownbox選擇的值到一個jQuery函數? 我必須參考我想在表單中使用的功能嗎?
  2. 我應該有一個提交按鈕嗎? (我不喜歡)

- 編輯 - 我的觀點是我想知道用戶選擇了哪些選項,以便我可以在另一個函數中使用它。

非常感謝!

+1

你到底要幹什麼? – 2010-11-15 09:41:13

+0

即時通訊不知道你的意思是「確認」按鈕,但你應該有某種「提交」按鈕。另外 - 你確定你想在這裏使用'method =「GET」'嗎? – RPM1984 2010-11-15 09:44:38

回答

3

這聽起來像你想立即提交更改。在這種情況下使用change event這樣的:

$(function() { //run when the DOM is ready 
    $("#countriesList").change(function() { //this runs when the selection changes 
    $.post("myPage.php", { country: $(this).val() }, function(data) { 
     //use data here 
    }); 
    }); 
}); 

這樣做是什麼,當你選擇的變化,我們發佈到"myPage.php"它獲取的country變量後,這將是你挑(allcountry等)。剛剛從$_POST["country"]得到這個,並回應你的迴應......這將是data在上面$.post()的回調。然後,你可以做任何你想要與響應,例如,如果它的另一個<select>與狀態,你可以追加,爲地方,例如:

$("#nextDropDownContainer").html(data); 
相關問題