iT邦幫忙

0

.Net MVC下製做RadioButton(使用razor)

請問,我現在有寫一個radio button用razor的方法
然後,目前是希望這三個選了其中一個後,至少會跳出一個alert,主要是確認我是否有抓到該選項內的值
○通過
○未通過(xxxxxxx)
○不適用(xxxxx)

寫法如下:
@Html.RadioButton("testResultRadioBtnName", testResult.TestResult, new { id = "testResultRadioBtnID"})@testResult.TestResult

我知道我寫錯了…因為我目前寫完後,RadioButton的id都長一樣(testResultRadioBtnID),而且只有點第一個「通過」會show出alert…我想要的是不管我選擇哪一個,都會show出alert,因為我最終的值是要存到db裡面,請各位前輩幫我一下,看怎麼做才是對的

如果有我講的不清楚的地方,再麻煩前輩跟我說,我再補充,謝謝!!

froce iT邦大師 1 級 ‧ 2020-03-31 04:31:30 檢舉
直接用js寫比較快。
var checked = document.querySelector('[name=testResultRadioBtnName]:checked')
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
ekkusu
iT邦新手 4 級 ‧ 2020-04-01 15:23:06

ID是一樣的是正常的
不然你後端要怎樣接收同一組radio的資料

    Male:   @Html.RadioButton("Gender", "Male",true)
    Female: @Html.RadioButton("Gender", "Female")

會產出

Male:   <input checked="checked" id="Gender" name="Gender" type="radio" value="Male">
Female: <input id="Gender" name="Gender" type="radio" value="Female">

重要的是你要給他們不同的value

你在後端接收的部分
就會是
public IActionResult Gender(string Gender)

我要發表回答

立即登入回答