2011-02-25 57 views
13

我想,而不是標準的單選按鈕,爲每個單選按鈕使用不同的圖像。對於選定的狀態,我希望邊框在圖像周圍出現。如何用圖像替換替換單選按鈕?

我已經嘗試製作單選按鈕的圖像標籤,然後隱藏按鈕,但似乎打破了功能出於某種原因。

我也遇到了這篇文章:http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/,我可能以某種方式扭曲我的目的。

有沒有更簡單/更好的方法?

回答

23

的jQuery

 $('input:radio').hide().each(function() { 
      $(this).attr('data-radio-fx', this.name); 
      var label = $("label[for=" + '"' + this.id + '"' + "]").text(); 
      $('<a ' + (label != '' ? 'title=" ' + label + ' "' : '') + ' data-radio-fx="'+this.name+'" class="radio-fx" href="#">'+ 
       '<span class="radio' + (this.checked ? ' radio-checked' : '') + '"></span></a>').insertAfter(this); 
     }); 
     $('a.radio-fx').on('click', function(e) { 
      e.preventDefault(); 
      var unique = $(this).attr('data-radio-fx'); 
      $("a[data-radio-fx='"+unique+"'] span").attr('class','radio'); 
      $(":radio[data-radio-fx='"+unique+"']").attr('checked',false); 
      $(this).find('span').attr('class','radio-checked'); 
      $(this).prev('input:radio').attr('checked',true); 
     }).on('keydown', function(e) { 
      if ((e.keyCode ? e.keyCode : e.which) == 32) { 
       $(this).trigger('click'); 
      } 
     }); 
    在演示源
  • 全碼;
+0

類似的東西,但這樣會起到很好的與Prototype和Scriptaculous – 2011-02-25 03:33:38

+0

你不需要對Scriptaculous的做到這一點,只是原型是對子級罰款,我覺得也不是那麼困難重寫我的代碼,以便與它一起工作。 – 2011-02-25 03:52:09

+0

我真的很感激這個幫助,我會積極/選擇這個答案作爲適當的解決方案,如果我今天找到時間來逐步完成解決方案 – 2011-02-25 15:15:12

1

我認爲你不會找到比你添加到問題中的鏈接更簡單的方法。這是我知道的唯一途徑。我認爲你有效地回答了你自己的問題。沒有這個或那個東西的徽章?

而且,類似的問題在這裏回答:Styling checkboxes, radio buttons and dropdowns

它包含幾個預先構建的解決方案,你可以看看。如果沒有更多關於你想要在視覺上完成什麼的信息,我不能說它們中的任何一個會爲你工作。

祝你好運!

2

我只有一小會兒前有同樣的問題,發現這個jQuery的解決方案,它完美的作品,並很好地降低:

http://www.adamcoulombe.info/lab/jquery/select-box/

我用它來定製樣式選擇下拉菜單。這很容易實現。例如,將目標類別爲$('.mySelectBoxClass')的變量替換爲$('select') - 這將針對您網頁上的所有選定元素。同樣的規則適用於單選按鈕。

3

我還沒有研究,但是,這個聽起來前途: http://www.thecssninja.com/css/custom-inputs-using-css

+1

+1 for pure- CSS解決方案。 Lea Verou描述了同樣的方法[在本演示中](http://lea.verou.me/css3-secrets/#custom-form-controls)。 – GeReV 2013-02-15 17:15:23

+0

剛剛嘗試過這個cssninja,它是迄今爲止我見過的最好的解決方案。主要優點是它不會與其他jquery單選按鈕操作衝突,如.prop('checked',true)就像aSeptik的答案一樣 – BaronVonKaneHoffen 2013-07-11 00:13:44

3

我提出了兩種不同的解決方案:

CSS SOLUTION:

/* 
    Hide radio button (the round disc) 
    we will use just the label to create push button effect 
*/ 
input[type=radio] { 
    display:none; 
    margin:10px; 
} 

/* 
    Change the look'n'feel of labels (which are adjacent to radio buttons). 
    Add some margin, padding to label 
*/ 
input[type=radio] + label { 
    display:inline-block; 
    margin:-2px; 
    padding: 4px 12px; 
    background-color: #e7e7e7; 
    border-color: #ddd; 
} 
/* 
Change background color for label next to checked radio button 
to make it look like highlighted button 
*/ 
input[type=radio]:checked + label { 
    background-image: none; 
    background-color:#d0d0d0; 
} 

與HTML

<input type="radio" id="radio1" name="radios" value="all" checked> 
    <label for="radio1">iPhone</label> 
<input type="radio" id="radio2" name="radios"value="false"> 
    <label for="radio2">Galaxy S IV</label> 
<input type="radio" id="radio3" name="radios" value="true"> 
    <label for="radio3">Nexus S</label> 

JAVASCRIPT SOLUTION

Demo Here

+0

很好的答案!感謝你們所有的例子。 – 2014-12-04 17:14:01

14

是的,有!這是簡單的方法的例子:

無需JavaScript的

CSS

.radio_item{ 
display: none !important; 
} 

.label_item { 
opacity: 0.1; 
} 

.radio_item:checked + label { 
opacity: 1; 
} 

HTML

<!--RADIO 1--> 
<input type="radio" class="radio_item" value="" name="item" id="radio1"> 
<label class="label_item" for="radio1"> <img src="img_url_here"> </label> 

<!--RADIO 2--> 
<input type="radio" class="radio_item" value="" name="item" id="radio2"> 
<label class="label_item" for="radio2"> <img src="img_url_here"> </label> 

FIDDLE

+0

我覺得這是最簡單的方法之一!非常感謝。 – mvsagar 2015-02-21 11:30:14

+0

這是一個很好的答案 – user3658423 2015-03-20 08:04:51

+0

最簡單的答案 – Gangesh 2016-06-29 22:32:47

1

aSeptik解決方案存在一個錯誤,如果您單擊一個選中的單選按鈕,它將取消選中它。我通過下面的調整來糾正它。

$('a.radio-fx').on('click', function(e) { 
     e.preventDefault(); 
     if($(this).prev('input:radio').is(':checked')) return; 
     ... 

所以......

$('input:radio').hide().each(function() { 
     $(this).attr('data-radio-fx', this.name); 
     var label = $("label[for=" + '"' + this.id + '"' + "]").text(); 
     $('<a ' + (label != '' ? 'title=" ' + label + ' "' : '') + ' data-radio-fx="'+this.name+'" class="radio-fx" href="#">'+ 
      '<span class="radio' + (this.checked ? ' radio-checked' : '') + '"></span></a>').insertAfter(this); 
    }); 
    $('a.radio-fx').on('click', function(e) { 
     e.preventDefault(); 
     if($(this).prev('input:radio').is(':checked')) return; 
     var unique = $(this).attr('data-radio-fx'); 
     $("a[data-radio-fx='"+unique+"'] span").attr('class','radio'); 
     $(":radio[data-radio-fx='"+unique+"']").attr('checked',false); 
     $(this).find('span').attr('class','radio-checked'); 
     $(this).prev('input:radio').attr('checked',true); 
    }).on('keydown', function(e) { 
     if ((e.keyCode ? e.keyCode : e.which) == 32) { 
      $(this).trigger('click'); 
     } 
    }); 

謝謝aSeptik了很好的解決方案!

0

非常簡單的解決方案,我在網上找到。

http://jsbin.com/image-instead-of-radio-button/3/edit?html,css,output

+0

這相當不錯!但是,在StackOverflow上,它只是把一個鏈接答案[(*參見這個meta post *)](http://meta.stackoverflow.com/questions/251006/flagging-link-only-answers)。如果您在帖子中解釋了一些解決方案,或者更好,請使用StackOverflows新的嵌入代碼段來包含一個可行的示例,然後您肯定會獲得我的讚賞。 (當然,留下鏈接作爲歸屬) – 2015-07-01 22:22:33