2013-04-29 51 views
1

我在網站上有一個頁面,它需要用戶進行一些選擇。由於該網站專用於移動用戶,因此我使用的是JQuery Mobile。該網站也由CMS管理,這意味着我需要從VB.net編寫的代碼隱藏文件生成頁面。Radiobuttonlist在視覺上允許多個選擇並鎖定到第一個選擇

現在對於用戶需要做出的每一種選擇,我在集體collapsible-set內製作了一個collapsible。對於某些選擇,生成一個單選按鈕列表。生成單選按鈕是通過一箇中繼器完成的,該中繼器會通過所有可能的選擇併爲每個選項生成一個單選按鈕。當用戶然後按下按鈕以確認他的選擇時,選擇的選項將被收集併發送到服務器進行處理。

現在,我已經盡了最大努力來調整中繼器和代碼隱藏以生成必要的標記和屬性,以允許JQuery Mobile設置其佈局,但這已導致生成的單選按鈕列表不再起作用他們應該。 每次加載頁面時,它都會自動檢查第一個選項,並將其鎖定到所述選項。另一方面,在視覺上,實際上可以選擇整個單選按鈕列表中的所有選項(當選擇另一個選項時,不會選擇任何選項)。這只是視覺上的(實際上,只有有史以來第一次選擇是在幕後進行檢查,並且在嘗試選擇其他時不會改變)。

我已經嘗試了一些關於列表的事情。我在轉發器周圍放置了一個field-set標籤,將所有生成的單選按鈕綁定在一起,並更改了代碼以確保name屬性是唯一的(該ID已經是唯一的)。以下是我檢查過的其中一個問題的快速鏈接。與我的問題沒有直接關係,但我認爲可以通過以下答案解決:Jquery mobile radio button selection

這裏的HTML的外觀在組件(預CMS代)的單選按鈕列表中的一個:

<div class="detailblock thumbs" runat="server" id="div_plants" data-role="collapsible"> 
    <asp:Literal ID="litTitle_plants" runat="server" />    
    <fieldset data-role="controlgroup"> 
     <asp:Repeater ID="repDetails_plants" runat="server"> 
      <ItemTemplate> 
       <asp:Literal ID="litValue" runat="server" Visible="false" /> 
       <asp:RadioButton ID="radPlant" runat="server" GroupName="plant" /> 
      </ItemTemplate> 
     </asp:Repeater> 
    </fieldset> 
</div> 

首先有一個包含工廠所有選項collapsible。第一個文字用於設置collapsible的標題。然後我們有field-set,它應該將這些單選按鈕綁定在一起。最後,我們有一箇中繼器,每個工廠產生一個無形價值,還有一個單選按鈕。

下面是處理每一個項目的生成代碼:

Protected Function ProcessProductPlant(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) As Boolean 
    Dim plant As em_clsProductSizeColorPlant = e.Item.DataItem 

    If plant Is Nothing Then 
     Return False 
    End If 

    Dim litValue As Literal = e.Item.FindControl("litValue") 
    litValue.Text = plant.id 

    Dim radPlant As RadioButton = e.Item.FindControl("radPlant") 
    If Not plant.Image Is Nothing Then 
     radPlant.Text = "<img src='" & plant.Image.ToLower & "' alt='" & plant.description & "' />" 
    Else 
     radPlant.Text = "<img src='" & "/websites/1/uploads/img/wapla/logo.jpg" & "' alt='" & plant.description & "' />" 
    End If 
    radPlant.Attributes.Add("name", "plant_choice_" & e.Item.ItemIndex) 

    If e.Item.ItemIndex = 0 Then 
     radPlant.Checked = True 
     litCurrentPlantId.Text = "var currentPlantId = '" & plant.id & "';" 
    End If 

    radPlant.Attributes.Add("onclick", "currentPlantId = '" & plant.id & "'; change_image();") 

    Return True 
End Function 

最後,這裏是如何植物可摺疊看起來HTML明智時產生:

<div id="ctl24_div_plants" class="detailblock thumbs" data-role="collapsible"> 
     <h2>1. choose a plant</h2> 
     <fieldset data-role="controlgroup"> 
      <span name="plant_choice_0"><input id="ctl24_repDetails_plants_ctl00_radPlant" type="radio" name="ctl24$repDetails_plants$ctl00$plant" value="radPlant" checked="checked"/><label for="ctl24_repDetails_plants_ctl00_radPlant"><img src='/websites/1/uploads/image/productimages/filrouge/plants/jpg/cycas_thumb.jpg' alt='BUSINESS 01' /></label></span> 
      <span name="plant_choice_1"><input id="ctl24_repDetails_plants_ctl01_radPlant" type="radio" name="ctl24$repDetails_plants$ctl01$plant" value="radPlant" /><label for="ctl24_repDetails_plants_ctl01_radPlant"><img src='/websites/1/uploads/image/productimages/filrouge/plants/jpg/sanseveria_kirkii_thumb.jpg' alt='BUSINESS 01' /></label></span> 
      <span name="plant_choice_2"><input id="ctl24_repDetails_plants_ctl02_radPlant" type="radio" name="ctl24$repDetails_plants$ctl02$plant" value="radPlant" /><label for="ctl24_repDetails_plants_ctl02_radPlant"><img src='/websites/1/uploads/image/productimages/filrouge/plants/jpg/zamioculcas_thumb.jpg' alt='BUSINESS 01' /></label></span>   
     </fieldset> 
    </div> 

注意如何一個產品已經檢查過。實際上,可以查看全部3個工廠,而HTML將僅保留第一個選擇。

如果您有任何需要更多信息來幫助我,請隨時詢問。歡迎所有幫助。

編輯

我拿起的地方,所有的單選按鈕組名的需要是相同的。在ItemTemplate中,我已經設置了組名,但在生成的HTML中沒有看到它。我是否需要通過代碼設置組名,或者組名是否應該在生成的HTML中不可見?

EDIT2

我試圖通過強制的單選按鈕刷新,懷疑最初的選擇,設置可能突破上籃出固定單選按鈕。我用下面這段jQuery的腳本的HTML文檔本身在做刷新(colorchoices是頁面上的第二組單選按鈕的):

$(document).ready(function() { 
    $("fieldset[id='plantchoices']").children("input").each("refresh"); 
    $("fieldset[id='colorchoices']").children("input").each("refresh"); 
}); 

注:這一段腳本並沒有解決我的問題。

回答

0

大量的試驗和錯誤之後,我已經成功地實現我想要的東西通過附加下面一段JavaScript代碼的onmouseup屬性每個單選按鈕的:

function uncheck_others(radiobuttonId) { 

     $('#' + radiobuttonId).parent().parent().parent().children().children().children('input').attr('checked', false); 
     $('#' + radiobuttonId).attr('checked', true).checkboxradio('refresh'); 
     $('#' + radiobuttonId).parent().parent().parent().children().children().children('label').removeClass('ui-radio-on'); 
     $('#' + radiobuttonId).parent().parent().parent().children().children().children('label').children().children().removeClass('ui-icon-radio-on'); 
     $('#' + radiobuttonId).parent().parent().parent().children().children().children('label').addClass('ui-radio-off'); 
     $('#' + radiobuttonId).parent().parent().parent().children().children().children('label').children().children().addClass('ui-icon-radio-off'); 
     $('#' + radiobuttonId).parent().parent().parent().children().children().children('label[for=' + radiobuttonId + ']').removeClass('ui-radio-off'); 
     $('#' + radiobuttonId).parent().parent().parent().children().children().children('label[for=' + radiobuttonId + ']').children().children().removeClass('ui-icon-radio-on'); 
     $('#' + radiobuttonId).parent().parent().parent().children().children().children('label[for=' + radiobuttonId + ']').addClass('ui-radio-on'); 
     $('#' + radiobuttonId).parent().parent().parent().children().children().children('label[for=' + radiobuttonId + ']').children().children().addClass('ui-icon-radio-off'); 
    } 

我意識到這個腳本是非常不切實際,無法讀取,並且可能會更有效率地解決此問題。我將以這種方式將腳本留在將來的參考中。

我還想指出,在我的具體情況下,此腳本必須附加到onmouseup事件,而不是onmouseclick事件。將它附加在onmouseup事件上導致JQuery將JS更改恢復爲前面提到的破壞的鎖定格式。

相關問題