2014-10-06 123 views
1

我对jQuery和JavaScript有点新鲜。我有3个图像正在使用这些方法打开和关闭。切换像单选按钮的图像

如何一次只切换一个,类似于单选按钮。

<form class="num-players-group" action=""> 
    <input type="hidden" id='testbutton'> 

    <a href="#" class="players2"> 
    <img src="Assets/Menu/DOR_players_2_off.png" style="display:inline-block" class="player-btn" alt="2 off" /> 
    <img src="Assets/Menu/DOR_players_2_on.png" style="display:none" class="player-btn" alt="2" /> 
    </a> 

    <a href="#" class="players3"> 
    <img src="Assets/Menu/DOR_players_3_off.png" style="display:inline-block" class="player-btn" alt="3 off" /> 
    <img src="Assets/Menu/DOR_players_3_on.png" style="display:none" class="player-btn" alt="3" /> 
    </a> 

    <a href="#" class="players4"> 
    <img src="Assets/Menu/DOR_players_4_off.png" style="display:inline-block" class="player-btn" alt="4 off" /> 
    <img src="Assets/Menu/DOR_players_4_on.png" style="display:none" class="player-btn" alt="4" /> 
    </a> 

</form> 

和我使用jQuery也:

$('.players2').click(function() { 
    $(this).find('img').toggle(); 
}); 
$('.players3').click(function() { 
    $(this).find('img').toggle(); 
}); 
$('.players4').click(function() { 
    $(this).find('img').toggle(); 
}); 

我应该使用沿着

$('.players4').not(this).removeClass('player-btn'); 
+0

你试过这个吗? http://stackoverflow.com/questions/17541614/use-thumbnail-image-instead-of-radio-button – 2014-10-06 07:23:18

回答

0

线的东西,你可以使用这样的事情

JS Fiddle

$('.num-players-group a').click(function() { 
    $(this).children('img').toggle(); 
}); 
+0

这个例子并不像单选按钮那样切换 – 2014-10-27 02:13:07

0

尝试像:(注:我已经简化你的HTML类选择):

$(function(){ // DOM ready 
 
    
 
    var $plBtn = $('a.players'); 
 
    
 
    $plBtn.click(function(e){ 
 
    e.preventDefault(); 
 
    $plBtn.removeClass('selected'); // Remove selected class from all 
 
    $(this).addClass('selected'); // Add to clicked Button 
 
    }); 
 
    
 
});
a.players{ 
 
    display:inline-block; 
 
    text-decoration:none; 
 
    border-radius:5px; 
 
    overflow:hidden; 
 
} 
 
a.players img{ 
 
    vertical-align:middle; 
 
} 
 
a.players img+img{ display:none; } /* Hide next sibling image */ 
 

 
a.players.selected img { display:none; } /* if selected hide red image */ 
 
a.players.selected img+img{ display:block; } /* and show the green one  */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form class="num-players-group" action=""> 
 
    <input type="hidden" id='testbutton'> 
 

 
    <a href="#" class="players"> 
 
    <img src="//placehold.it/40x40/f00&text=2" alt="2 off" /> 
 
    <img src="//placehold.it/40x40/0ff&text=2" alt="2" /> 
 
    </a> 
 

 
    <a href="#" class="players"> 
 
    <img src="//placehold.it/40x40/f00&text=3" alt="3 off" /> 
 
    <img src="//placehold.it/40x40/0ff&text=3" alt="3" /> 
 
    </a> 
 

 
    <a href="#" class="players"> 
 
    <img src="//placehold.it/40x40/f00&text=4" alt="4 off" /> 
 
    <img src="//placehold.it/40x40/0ff&text=4" alt="4" /> 
 
    </a> 
 

 
    </form>

P.S:选择+选择下一个同级元素。

+1

这很完美!我知道这可能不是最优雅的做法,但这是我能够正常工作的唯一方法。谢谢! – 2014-10-13 00:24:17

+0

@ShereeDillon不用客气 – 2014-10-13 04:04:45