簡體   English   中英

獲取單選按鈕列表的選定值

[英]Get the selected value of a list of radio buttons

我有一個具有相同name屬性的單選按鈕列表。 以下JavaScript旨在檢測任何按鈕中的更改,並將所選值傳遞給getColumnNames()方法。

$("input[name=SelectedSheet]:radio").change(function () {
    getColumnNames(this.val());
})

它檢測何時任何按鈕發生更改,但不傳遞所選單選按鈕的字符串值。

在Firebug中,單擊單選按鈕之一時, this關鍵字包含以下內容:

input#SelectedSheet_Second Sheet attribute value = "Second Sheet"

順便說一句, SelectedSheet是單選按鈕列表的name屬性。

如何將所選屬性的值傳遞給getColumnNames()方法?

.val()是一個jQuery方法,所以,必須用this$()使用它:

getColumnNames($(this).val());

或者,您可以只使用普通的JS:

getColumnNames(this.value);

您可以刪除:radio,因為它將通過名稱找到它。 然后使用$(this).val()代替

<input type="radio" name="SelectedSheet" value="sheet1" />
<input type="radio" name="SelectedSheet" value="sheet2" />
<input type="radio" name="SelectedSheet" value="sheet3" />

$(document).ready(function () {
   $('input[name="SelectedSheet"]').change(function () {
      alert($(this).val());
   });
});

http://jsfiddle.net/fxsupvhj/

嘗試使用$(this).val(); 在您的代碼中。

$("input[name=SelectedSheet]:radio").change(function () {
    getColumnNames($(this).val());
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM