2014-02-26 36 views
1

說我有這樣一組的三個單選按鈕:如何以編程方式刷新jQuery Mobile中的一組單選按鈕?

<label for="a-1">A</label> 
<input type="radio" name="a" id="a-1" value="1" /> 
<label for="a-2">A</label> 
<input type="radio" name="a" id="a-2" value="2" /> 
<label for="a-3">A</label> 
<input type="radio" name="a" id="a-3" value="3" /> 

與第一無線電設定爲checked="true" ALA

$("input").eq(0).attr("checked", true); 

JQM API for checkboxradio說以下內容:

$(".selector").prop("checked", true).checkboxradio("refresh"); 

以編程方式設置單選按鈕。

問題
如果我需要收音機從一個按鈕,切換到另一個?所以....

$("input").eq(1).trigger("click"); 

是否總是需要單獨更新我的點擊處理我的單選按鈕或不應該這項工作開箱?畢竟,當我有一個非JQM單選按鈕組並觸發單選單元時,先前選中的按鈕將自動取消選中。

謝謝!

編輯:
平原例如:

var foo = '<input type="radio" checked="checked" name="a" id="a-1" value="1" />' + 
      '<input type="radio" name="a" id="a-2" value="2" />' + 
      '<input type="radio" name="a" id="a-3" value="3" />'; 
$("div.ui-content").append($(foo)); 
$(document.getElementById("a-2")).trigger("click"); 

這產生三個單選按鈕,第一個被選擇。在點擊時,第二臺收音機正在被選中,並且第一臺收音機未被選中。

EDIT
JQM實施例:

$("div.ui-content").empty() 
var bar = '<div data-role="controlgroup">' + 
     '<input type="radio" name="a" id="a-1" value="choice-1" checked="checked">' + 
     '<label for="a-1">1</label>' + 
     '<input type="radio" name="a" id="a-2" value="choice-2">' + 
     '<label for="a-2">2</label>' + 
     '<input type="radio" name="a" id="a-3" value="choice-3">' + 
     '<label for="a-3">3</label>' + 
     '</div>'; 
$("div.ui-content").append($(bar)).enhanceWithin(); 
$("input").eq(2).trigger("click").checkboxradio("refresh"); 

這將導致在2個選擇單選按鈕。

+0

究竟你想做?動態選擇一個單選按鈕而無需刷新收音機組,對嗎? – TheMohanAhuja

+0

不選擇,但觸發點擊。在普通的HTML中,C上的'trigger(「click」)'取消了收音機A的選擇,而JQM則沒有。見http://jsbin.com/ofuhaw/1202 – frequent

回答

4

我按照您的link只需更換這

$(document).on("click", "#btn2", function() { 
    //$("#b-3").trigger("click").checkboxradio("refresh"); 
    $("[for=b-3]").trigger("click"); 
}); 

它沒有招我

+0

第一個仍然被選中。 – Omar

+0

@omar我已經通過下載文件檢查了這一點,但「頻繁」可能沒有更新代碼到目前爲止。 – TheMohanAhuja

1

TheMohanAhuja給予了正確的答案。在jQM增強複選框之後,它將響應點擊標籤元素而不是輸入。

所以你在OP提供的錯誤代碼,只需更改最後一行來觸發標籤上點擊:

$("div.ui-content").empty(); 
var bar = '<div data-role="controlgroup">' + 
    '<input type="radio" name="a" id="a-1" value="choice-1" checked="checked">' + 
    '<label for="a-1">1</label>' + 
    '<input type="radio" name="a" id="a-2" value="choice-2">' + 
    '<label for="a-2">2</label>' + 
    '<input type="radio" name="a" id="a-3" value="choice-3">' + 
    '<label for="a-3">3</label>' + 
    '</div>'; 
$("div.ui-content").append($(bar)).enhanceWithin(); 

$(".ui-radio label").eq(2).trigger("click"); 

這裏是一個DEMO

+0

不錯的訣竅......我還提出了JQM上的一個錯誤,這個錯誤已經被修復[pr](https://github.com/jquery/jquery-mobile/pull/7183) – frequent

相關問題