単純なjQueryif/ else if条件がありますが、「if」部分のみが機能し、「else if」は機能しないため、理由がわかりません。このコードは3つのリストボックス(ajax-populated)を表しており、いずれかがクリックされると、jQuery関数がクリックされたアイテムのIDを収集し、それを変数に割り当てます。最初のもの(場所を切り替えても正しいID番号を取得します)は問題ありません。2番目または3番目は問題ありません。「if/elseif」構造の代わりに3つの「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>