2011-12-05 59 views
2

我有兩列單選按鈕和一個下拉列表以供其他選擇。這兩個控件都是同一個項目的選項,例如本例中的「專業」。如果選擇了左列或右列中的一個單選按鈕,或者下拉選擇了下拉選項,如何從單選按鈕獲取選定值被選中並將該選擇作爲查詢字符串傳遞給URL?因此,如果心臟病專家從單選按鈕選擇,我想將它傳遞給URL作爲將單選按鈕選擇或下拉選擇傳遞給URL中的查詢字符串

/search/pages/physicians.aspx?v=relevance&s=Physicians&k=Cardiologist 

,或者如果數字1從下拉選擇,則該URL被附加

/search/pages/physicians.aspx?v=relevance&s=Physicians&k=number1 

如何在jQuery中這樣做?

<div class="Specialty-Choices"><span class="Choices-Title">Choose a Specialty</span><br /><br /> 
<div class="radioHolder"> 
    <div class="radioCol-left"> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Cardiologist"/> <label>Cardiologist</label> </div> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Dermatologist"/> <label>Dermatologist</label> </div> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Family Physician"/> <label>Family Physician</label> </div> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Gastroenterologist"/> <label>Gastroenterologist</label> </div> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Neurologist"/> <label>Neurologist</label> </div> 
    </div> 
    <div class="radioCol-right"> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Obstetrician"/> <label>Obstetrician</label> </div> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Orthopedic Surgeon"/> <label>Orthopedic Surgeon</label> </div> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Pediatrician"/> <label>Pediatrician</label> </div> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Primary Care Doctor"/> <label>Primary Care Doctor</label> </div> 
     <div class="radioRow"><input name="speciality_type" type="radio" value="Psychiatrist"/> <label>Psychiatrist</label> </div> 
    </div> 
    </div><br /> 
    <div class="mainPaneRow clearfix"><span class="inp"><select id="landing_physician_specialty"> <option disabled="disabled" selected="selected">Select other specialty</option> <option>--number-1--</option> <option>--number-2--</option> <option>--number-3--</option></select> </span></div> 
    <div class="mainPaneRow clearfix"><div class="submitBox"><a class="btnBlue" href="#" type="submit"><span><em class="searchElem">Search</em></span></a> <a class="paneBoxLink" href="/physicians/">Advanced Search</a> </div></div> 
</div><!--END Specialty-Choices--> 

回答

3

要獲取選定的單選按鈕:

var selectedDoctorType = $("input[name='speciality_type']:checked").val(); 

獲取所選的下拉值:

var selectedNum = $("#landing_physician_specialty").val(); 

讓他們到一個URL應該只是做了一些字符串串聯的問題

var baseUrl = "search/pages/physicians.aspx?v=relevance&"; 

if (selectedDoctorType) 
    baseUrl += "s=" + selectedDoctorType + "&"; 
if (selectedNum) 
    baseUrl += "v=" + selectedNum; 

請注意,我的解決方案中潛在的amersand是perfectly valid

相關問題