문제 :PHP와 jQuery로 선택에 따라 배열의 수와 텍스트 필드에 숫자를 비교
이 라디오 버튼을 선택하고 배열의 숫자가있는 텍스트 필드의 숫자를 비교 하였다. true이면 아무 작업도 수행하지 않고 다른 div로 div를 전환합니다.
HTML 코드 :
<!-- Step 1 - Choose level -->
<label class="control-label">Level:</label>
<label class="radio"><input type="radio" name="level" id="level" value="1">C-level</label>
<label class="radio"><input type="radio" name="level" id="level" value="2">D-level</label>
<!-- Step 2 - This apply only if choice 1 (C-level) is chosen -->
<label class="control-label">Number of authors:</label>
<label class="radio"><input type="radio" name="authors" id="authors" value="1">1 author</label>
<label class="radio"><input type="radio" name="authors" id="authors" value="2">2 authors</label>
<!-- Step 2 - This apply only if choice 2 (D-level) is chosen -->
<label class="control-label">Credits:</label>
<label class="radio"><input type="radio" name="credits" id="credits" value="1">15 credits</label>
<label class="radio"><input type="radio" name="credits" id="credits" value="2">30 credits</label>
<!-- Step 3 - This apply for both choice 1 and 2 -->
<label class="control-label">Type of study:</label>
<label class="radio"><input type="radio" name="study" id="study" value="1">Within</label>
<label class="radio"><input type="radio" name="study" id="study" value="2">Between</label>
<!-- Check the number in this input field against the criteria in $within or $between -->
<label class="control-label">Participants:</label>
<input class="input-small" id="participants" name="participants" type="text">
JQuery와 코드 :
<script type="text/javascript">
$(document).ready(function()
{
var numbers = {
'1': { // whithin
'1': { // C
'1': 36,
'2': 63
},
'2': { // D
'1': 54,
'2': 63
}
},
'2': { // between
'1': { // C
'1': 60,
'2': 105
},
'2': { // D
'1': 90,
'2': 105
}
// ...
}
};
$('#participants').change(function()
{
var choice1 = $('input[name=level]:checked').val();
if (choice1 == '1')
{
var choice2 = $('input[name=authors]:checked').val();
}
else
{
var choice2 = $('input[name=credits]:checked').val();
}
var choice3 = $('input[name=study]:checked').val();
var number = numbers[choice3][choice1][choice2];
if ($(this).val() < number)
{
$('#part').addClass('error');
}
else
{
$('#part').removeClass('error');
}
}
});
</script>
확장 설명 :이
아이디어는 각 선택의 값을하는 것입니다 그것들을 더해서 th에 해당하는지 확인하십시오. e 배열에 주어진 값.
시나리오 :
- 이용자 "C 수준" 선택 (값 = 1), 다음 연결될 때, 12과 동일 "2 개 저자" (= 2 값)를 선택한다.
- 사용자는 (값 = 2) 사이에 을 선택하고 텍스트 필드 (id/name = number)에 을 입력합니다.
- 사용자가 텍스트 필드에서 포커스를 이동하면 jQuery를 비교해야합니다.
결과 :
JQuery와 사이의 배열 $ 들어가는 값 (12)을 보이는 105 95 DIV 2 바꿔야 이하이므로 105 DIV 1과 동일하지 않은 그것의 참조 (사용자가 입력 한 값을 유지하면서).
DIV 코드 :
<div id="part" class="control-group">
<label class="control-label">Participants:</label>
<div class="controls">
<div class="input-prepend">
<input class="input-small" id="participants" name="participants" type="text">
</div>
</div>
</div>
사용자 다른 값의 유형과 이동이 집중 경우, 재 계산을 수행해야합니다. 텍스트 필드의 값이 배열의 값보다 크거나 같으면 아무 것도하지 않습니다.
아무런 도움이되지 않아 정말 고생하지 않으면 입력하지 않았을 것입니다! 미리 감사드립니다.
는 귀하의 제안에 따라 코드를 업데이트해야하지만 그것이 작동 할 수 없습니다 : 그 PHP 배열이 동적 인 경우
, 당신은 함수로 json_encode를 사용하여 자바 스크립트 배열을 설정할 수 있습니다. 어떤 조언도 환영합니다 – kexxcream
미안 해요, 나는 ");" 마지막에 change() 이벤트 리스너 함수를 닫아야한다. –
잘 작동하지만 또 다른 관련 문제가있다.addClass ('error')가 끝나면 "yourDiv"의 첫 번째 입력 요소를 찾아 이미지를 추가하고 조건이 충족되면 이미지를 제거 할 수 있습니까? 예를 들면 다음과 같습니다. – kexxcream