[英]get value of radio button
我在服務器端在運行時創建了單選按鈕
RadioButton button1 = new RadioButton();
button1.ID = question.Name + "_Radio1";
button1.Text = "Yes";
RadioButton button2 = new RadioButton();
button2.ID = question.Name + "_Radio2";
button2.Text = "No";
if (question.IsDynamic)
{
button1.Attributes.Add("onclick", "return updateQuestions('" + button1.ID + "')");
}
然后在Java腳本中,我嘗試獲取單選按鈕的值
function updateQuestions(controlName) {
var userAnswer;
if (controlType == "RadioButton") {
userAnswer = document.getElementById(controlName).innerHTML;
}
並且userAnswer為空。如何獲取單選按鈕的值???
獲得value
:
userAnswer = document.getElementById(controlName).value;
還可以考慮將id以外的東西傳遞給函數。 如果該值是您要查找的值,則只需傳遞該值即可。 這些方法中的任何一種都可以將值直接傳遞給函數:
button1.Attributes.Add("onclick", "return updateQuestions('someValue')");
要么
button1.Attributes.Add("onclick", "return updateQuestions(this.value)");
編輯:另外,如果你沒有明確設置在控制服務器端的值,這個值是空字符串,所以你看到的是正確的。 在創建控件時,通過轉到button1.Value = "myValue";
設置值button1.Value = "myValue";
。
RadioButton button1 = new RadioButton();
button1.ID = question.Name + "_Radio1";
button1.Text = "Yes";
button1.Attributes["value"] = "Yes";
嘗試:
userAnswer = document.getElementById(controlName).value;
更改
button1.Attributes.Add("onclick", "return updateQuestions('" + button1.ID + "')");
至
button1.Attributes.Add("onclick", "return updateQuestions('" + button1.ClientID+ "')");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.