我有问题。我的当前代码在select元素发生更改之前不会获取数据。这就是为什么我在更改选项卡时尝试强制选择的原因(因为我的.change函数需要来自selectorMRPC();
的参数。但是我的代码在某种程度上$('#selectMRPC').trigger('change');
首先执行而不是selectorMRPC();
并且即使.trigger在函数selectorMRPC();
如何在第一个项目的选择器上强制更新?运行函数selectorMRPC();
function selectorMRPC() {
$.ajax({
type: 'post',
url: 'IndividualSimulator',
success: function (result) {
$('#selectMRPC').empty();
JSON.parse(result).forEach(function (mrpc) {
var option = $('<option>', {
value: mrpc.name,
text: mrpc.name
});
//Persist data with option
option.data('mrpc', mrpc);
console.log(mrpc);
$('#selectMRPC').append(option);
});
},
error: function () {
}
});
return false;
}
//Execute MRPC
$('#runMRPC').click(function (e) {
e.preventDefault();
//Get data
var data = $('#mrpcData').serialize();
$.ajax({
type: 'post',
url: 'jdbcMRPC',
data: data,
success: function (result) {
//Change to result tab
showResult(result);
},
error: function (xhr) {
showError(xhr.responseText);
}
});
});
$('#selectMRPC').change(function () {
//fetch data
var mrpc = $(this).find('option:selected').data('mrpc');
$('#paramBody').empty();
for (var i = 1; i <= 10; i++) {
var field = mrpc["field" + i];
if (field !== undefined) {
var parsedField = field.split('_');
var value = parsedField[0];
var type = parsedField[1];
switch (type) {
case "S":
type = "text";
if (value === '""')
value = null;
break;
case "B":
type = "checkbox";
break;
case "N":
type = "number";
value = parseInt(value);
break;
case "D":
type = "number";
value = parseInt(value);
if (value === '""')
value = null;
break;
}
//else use checkbox
if(type === "checkbox")
{
$('#paramBody').append('<tr><td>' + i + '</td><td>' + type + '</td><td><div class="checkbox checkbox-primary">' +
'<input class="text-center" id="field'+i+'" name="Fields" type="checkbox" value="' +value + '"><label for="field'+i+'"></label></div></tr>');
}
else
{
$('#paramBody').append('<tr><td>' + i + '</td><td>' + type + '</td><td><input class="text-center" name="Fields" type="' + type + '" value="' +value + '"></tr>');
}
}
}
});
$(".next-step").click(function (e) {
var $active = $('.wizard .nav-tabs li.active');
$active.next().removeClass('disabled');
nextTab($active);
//if it's the second tab, refresh it
var liID = $('#menu').find('.active').attr('id');
if(liID === "parametersTab") {
$("#selectMRPC").val($("#selectMRPC option:first").val());
selectorMRPC();
$('#selectMRPC').trigger('change');
}
});
答案 0 :(得分:1)
你可以从selectorMRPC返回$ .ajax的结果,然后在
中selectorMRPC().then(function(){
$('#selectMRPC').trigger('change');
});
或使用回调函数,如果在填充select之前仍然触发
selectorMRPC(function(){
$('#selectMRPC').trigger('change');
});