간단한 if/else if 조건이 있지만 "if"부분 만 작동하고 "else if"부분은 작동하지 않습니다. 이유를 찾아라. 이 코드는 세 개의 목록 상자 (ajax가 채워진)를 나타냅니다.이 중 하나를 클릭하면 jQuery 함수가 클릭 된 항목의 ID를 수집하고이를 변수에 할당합니다. 첫 번째 (올바른 ID 번호를 얻은 장소를 전환하더라도) 두 번째 또는 세 번째 절대로 괜찮습니다. "if/else if"구조 대신에 "if"를 3 개 사용하면 작동하지 않습니다.jQuery if/else if 문이 처음 "if"후에 작동하지 않습니다.
$(document).ready(function() {
$('#btn_edit_details').click(function() {
var request_id;
if ($('#lstbx_new_repeat_requests').val().length > 0) {
request_id = $('#lstbx_new_repeat_requests').val();
alert(request_id);
}
else if ($('#lstbx_approved_repeat_requests').val().length > 0) {
request_id = $('#lstbx_approved_repeat_requests').val();
alert(request_id);
}
else if ($('#lstbx_declined_repeat_requests').val().length > 0) {
request_id = $('#lstbx_declined_repeat_requests').val();
alert(request_id);
}
if ($('#btn_edit_details').val() == 'edit') {
// rest of the code, working fine when the request_id is passed from the first "if"
그리고 HTML
<input type='button' id='btn_edit_details' value='edit' />
<select id='lstbx_new_repeat_requests' name='lstbx_new_repeat_requests'></select>
<select id='lstbx_approved_repeat_requests' name='lstbx_approved_repeat_requests'></select>
<select id='lstbx_declined_repeat_requests' name='lstbx_declined_repeat_requests'></select>
을 처음'if' 문이 항상하는 경우 사실 .. 그것은 else else 문으로 결코 전달되지 않을 것입니다. –
항상 그렇지는 않습니다 - 목록 상자의 val()은 클릭 할 때만 비어 있지 않습니다. . 또는 나는 무엇인가 놓치고 있냐? – Salty
'if ($ ('# lstbx_new_repeat_requests') .val(). 길이> 0)'에서'if (false)'로 변경하면 다른 'else if'로 가야합니다. val(). length)' – Jashwant