![](/img/trans.png)
[英]How to Find selected item from radiobutton list using javascript
[英]How to findout selected radiobutton from radiobutton list which is created dynamically?
嘿,我正在處理在線考試表格,在其中我創建了一個帶有多項選擇題的論文,因此對於此創建的動態標簽和單選按鈕列表,我現在嘗試檢查用戶選擇的答案是否正確,為此我創建了一個包含所有內容的列表其中的單選按鈕列表和所有正確答案的列表,當用戶按下“提交”按鈕時->
dim answerList as List(Of String)
dim radionButtonList as List(Of RadioButtonList)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For i As Integer = 0 To radioButtonList.Count - 1
if String.Compare(radioButtonList(i).SelectedItem.ToString(), answerList(i)) <> 0 than
MsgBox("Wrong Answer")
End If
Next
End Sub
但是,當我單擊按鈕時出現錯誤“ System.NullReferenceException:對象引用未設置為對象的實例。 ”我猜按按鈕時頁面會刷新,所以我知道如何解決嗎?
好吧,我自己弄清楚,這只能通過使用javascript來實現。 所以我在asp.net中編寫js代碼,並在運行時使用文檔模型的兩個函數document.getElementById()
和document.getElementsByTagName()
注冊它
scriptText &= "function checkRadio(){" + _
"var sel = new Array();"
scriptText &= "for(var i = 1; i <= 3; i++) {" + _
"var ele = document.getElementById('MainContent_RadioButtonList' + i);" + _
"for(var j = 0; j < 4; j++) {" + _
" if(ele.getElementsByTagName('input')[j].checked) { " + _
" sel[i] = ele.getElementsByTagName('input')[j].value; console.log(sel[i]);}}} "
scriptText &= ""
scriptText &= "}"
ClientScript.RegisterClientScriptBlock(Me.GetType(), _
"CounterScript", scriptText, True)
“ MainContent_”是我的內容所有者,因此如果您沒有內容所有者,則可以簡單地放置“ RadioButtonList”,或者您可以簡單地使用getElementsByTagName(),但是它將為您提供帶有輸入標簽的對象,例如按鈕,文本字段,單選等。您必須先檢查類型,然后再進行處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.