简体   繁体   中英

jQuery Selector set select value not working

I have a bootstrap modal, that is offers users to add or edit bank account data.

When I click "Edit" Btn to edit bank data, the modal will set the value to the input value and select the option.

But in the modal, I have two select options, top of the select is to choose bank code,

and when the select changes, it will trigger change function to use ajax to get branch bank list.

So if I want to set the value to the modal, the top select option (bank) can successfully set value,

but the bottom select option (branch) will not successfully set the value to the modal.

My code is:

// Set top select option
$('#selectBankList option[value='+bankName[0]+']').attr('selected','selected');

// Show Modal
addBankModal.show();

// getBranchList
f_getBranchList(bankName[0]);

$(document).ready(function() {
  // Set bottom select option
  $('#selectBranchName option[value='+branchName+']').attr('selected','selected');
});

I've to try putting the set #selectBranchName value to anywhere, but still not working.

So, where is my code wrong?

$('#id attribute[foo="bar"]');

你没有添加内部引号

$('#selectBankList ').on('change', function(){
    $('#selectBranchName option[value="'+$(this).val()+'"]').attr('selected', 'selected').addClass('active');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM