2017-09-19 1 views
0

경고 선택한 라디오 버튼 여기 <code>form</code>에서

$('form').change(function() { 
 
    if('input[type=radio]:checked') { 
 
     alert('Already Selected'); 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form method='get' action=''> 
 
    <input type="radio" name="radiobutton"> 
 
    <input type="radio" name="radiobutton"> 
 
    <input type="radio" name="radiobutton"> 
 
</form>

클릭 메시지, 내가 선택한 radio 버튼을 두 번 클릭에서 사용자를 방지하기 위해 노력하고있어, 그래서 내가 alert 메시지에 원하는 선택한 radio 버튼을 누를 때마다 당신의 일반 영어 설명에 따라

답변

1

그래서 몇 가지 :

  1. 당신은 아마 대신 폼 요소의 라디오 입력에 결합한다.
  2. 당신은 아마 당신은 각 라디오 버튼

$('input[type=radio]').click(function() { 
 
    if($(this).data('previously-checked')) { 
 
     alert('Already Selected'); 
 
    } 
 
    // update all as not clicked 
 
    $('input[type=radio]').data('previously-checked', false); 
 
    // mark current one as checked 
 
    $(this).data('previously-checked', true); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form method='get' action=''> 
 
    <input type="radio" name="radiobutton" value="Y"> 
 
    <input type="radio" name="radiobutton" value="Y"> 
 
    <input type="radio" name="radiobutton" value="Y"> 
 
</form>

의 상태를 추적 할 필요가 대신 변화
  • 의 클릭 이벤트를 사용해야합니다
  • 관련 문제