2010-07-17 46 views
0

更新: 問題更復雜。看起來像它涉及手風琴頭沒有正確傳遞.click事件,並且與檢查選擇器無關。所有這個例子都在測試文件中正常工作。jQuery不返回檢查的電臺值

請參閱Radio input in header of accordion problem ?BUG

我有幾個無線電元素形式中,每個名字 「RX」(其中x爲1..3),.buttonset()施加。 與

比如:id = 3

alert ($('input:radio[name=r'+id+']').val()); 
//returns value of the first radio in the set 

alert ($('input:radio[name=r'+id+']:checked').val()); 
//returns 'undefined', even one of the radios is checked. 

,我都試過:checked')[0]:checked').get(0):checked').attr('id')

我不知道爲什麼第一個方法返回一個值......這是否有什麼關係.buttonset添加類/元素?

任何意見是讚賞!

PS。 $('input:radio [name = r'+ id +']')。get(0) 返回[object HTMLInputElement],我想它應該的方式。但是這個對象沒有.html()或.val()方法。 $('input:radio [name = r'+ id +']:checked')。get(0)仍然是'undefined'。

PPS。強制隱藏UI-helper的可見性 buttonset不會爲原始無線電元素「更改」。 我想我可以使用.ui-state-active來檢查活動收音機,而不是...有什麼更好的方法?

+0

@ selytch你需要什麼o/p? – 2010-07-17 07:56:02

+0

是的,謝謝你告訴我們你有手風琴.... – redsquare 2010-07-17 08:32:56

回答

1

原因。例如:

<input type="radio" name="r1" value="1" checked="checked" /> 
<input type="radio" name="r2" value="2" /> 

現在:

alert($('input:radio[name=r1]').val()); // alerts 1 
alert($('input:radio[name=r1]:checked').val()); // alerts 1 
alert($('input:radio[name=r2]').val()); // alerts 2 
alert($('input:radio[name=r2]:checked').val()); // alerts undefined as the button is not selected 
+0

我明白這是如何它應該工作,可以添加.buttonset被搞砸的事情? – selytch 2010-07-17 07:42:17

0

嘗試它沒有返回值,因爲當你使用:checked選擇,如果不選擇電臺也不會是結果集的一部分,該單

$("input[name='radio_name']:checked").val(); 
+1

未捕獲的語法錯誤,無法識別的表達式:[@ name = r1] – selytch 2010-07-17 07:37:10

+0

輸入錯誤,現在好了 – Sadat 2010-07-17 07:41:09

+0

'@'在jQuery 1.2中被棄用並且在jQuery 1.3中被移除 - http://api.jquery.com/category/selectors/attribute - 選擇器/ – 2010-07-17 07:41:43

0

閱讀頁關於選擇,尤其是這一個http://api.jquery.com/attribute-starts-with-selector/

alert ($(':radio[name^=r]:checked').val()); 

應該返回檢查按鈕的價值,或者不確定如果選擇了沒有。

+0

問題是,我有幾個按鈕組r1,r2,r3等... – selytch 2010-07-17 07:41:45

+0

我知道這似乎是一個簡單的問題,但可以.buttonset()影響的東西或其代碼在我的代碼錯誤? – selytch 2010-07-17 07:46:18

+0

嗯...閱讀我給你的選擇器頁面。匹配'r','r1','r2','r3','rWhatever'等等,只需要替換你需要的選擇符。問題不在於JQuery,而在於如何使用它。盟友稱爲RTFM – 2010-07-17 08:02:40

0

我寫了一個jQuery插件來設置和獲取單選按鈕值。它也尊重他們的「變化​​」事件。

(function ($) { 

    function changeRadioButton(element, value) { 
     var name = $(element).attr("name"); 
     $("[type=radio][name=" + name + "]:checked").removeAttr("checked"); 
     $("[type=radio][name=" + name + "][value=" + value + "]").attr("checked", "checked"); 
     $("[type=radio][name=" + name + "]:checked").change(); 
    } 

    function getRadioButton(element) { 
     var name = $(element).attr("name"); 
     return $("[type=radio][name=" + name + "]:checked").attr("value"); 
    } 

    var originalVal = $.fn.val; 
    $.fn.val = function(value) { 

     //is it a radio button? treat it differently. 
     if($(this).is("[type=radio]")) { 

      if (typeof value != 'undefined') { 

       //setter 
       changeRadioButton(this, value); 
       return $(this); 

      } else { 

       //getter 
       return getRadioButton(this); 

      } 

     } else { 

      //it wasn't a radio button - let's call the default val function. 
      if (typeof value != 'undefined') { 
       return originalVal.call(this, value); 
      } else { 
       return originalVal.call(this); 
      } 

     } 
    }; 
})(jQuery); 

將代碼放在任何地方以啓用插件。然後享受!它只是覆蓋默認的val函數而不會破壞任何東西。

你可以訪問這個jsFiddle來嘗試它的行動,並看看它是如何工作的。

http://jsfiddle.net/ffMathy/MN856/