Javascript 如果值或ajax结果为空,则出现错误

Javascript 如果值或ajax结果为空,则出现错误,javascript,jquery,ajax,html,Javascript,Jquery,Ajax,Html,我的问题是当emp_presentEmployment字段为空时,我的ajax脚本不工作,但如果我在数据库中对其赋值,我的代码工作 有人能帮我解决这种错误吗 问题: $('.prof_presentEmployed[value='+result.emp_presentEmployment+']').prop('checked', true).trigger("click"); $(document).ready(function() { $('#pro_empinfo, #pro_em

我的问题是当
emp_presentEmployment
字段为空时,我的ajax脚本不工作,但如果我在数据库中对其赋值,我的代码工作

有人能帮我解决这种错误吗

问题

$('.prof_presentEmployed[value='+result.emp_presentEmployment+']').prop('checked', true).trigger("click");
$(document).ready(function() {
    $('#pro_empinfo, #pro_empinfo1, #pro_empinfo2, #pro_empinfo3, #pro_empinfo4, #pro_empinfo5').click(function() {

        var data = {};
        data.emailCodeResult = $('#emailCodeResult').val();

        $.ajax({
            type: "POST",
            url: "Oppa/view/viewEmployment.php",
            data: data,
            cache: false,
            dataType: "JSON",
            success: function(result) {

                $('.prof_employed[value=' + result.emp_currentlyEmployed + ']').prop('checked', true).trigger("click");
                $('.prof_presentEmployed[value=' + result.emp_presentEmployment + ']').prop('checked', true).trigger("click");
                $("#prof_officeName").val(result.emp_officeName);
                $("#prof_officeAddress").val(result.emp_officeAddress);
                $("#prof_jobPosition").val(result.emp_jobPosition);
                $("#prof_noYearsInPosition").val(result.emp_noOfYearsinPresentJob);
                $("#prof_noYearsInGovPosition").val(result.emp_totalYearsinGovernmentService);
                $("#prof_statusOfEmployment").val(result.emp_statusOfEmployment);
                $(".show-page[data-page=Profile_Employment_info]").trigger("click");

            }
        });
        return false;

    });
});
脚本

$('.prof_presentEmployed[value='+result.emp_presentEmployment+']').prop('checked', true).trigger("click");
$(document).ready(function() {
    $('#pro_empinfo, #pro_empinfo1, #pro_empinfo2, #pro_empinfo3, #pro_empinfo4, #pro_empinfo5').click(function() {

        var data = {};
        data.emailCodeResult = $('#emailCodeResult').val();

        $.ajax({
            type: "POST",
            url: "Oppa/view/viewEmployment.php",
            data: data,
            cache: false,
            dataType: "JSON",
            success: function(result) {

                $('.prof_employed[value=' + result.emp_currentlyEmployed + ']').prop('checked', true).trigger("click");
                $('.prof_presentEmployed[value=' + result.emp_presentEmployment + ']').prop('checked', true).trigger("click");
                $("#prof_officeName").val(result.emp_officeName);
                $("#prof_officeAddress").val(result.emp_officeAddress);
                $("#prof_jobPosition").val(result.emp_jobPosition);
                $("#prof_noYearsInPosition").val(result.emp_noOfYearsinPresentJob);
                $("#prof_noYearsInGovPosition").val(result.emp_totalYearsinGovernmentService);
                $("#prof_statusOfEmployment").val(result.emp_statusOfEmployment);
                $(".show-page[data-page=Profile_Employment_info]").trigger("click");

            }
        });
        return false;

    });
});

您应该在尝试使用该属性之前检查它是否存在,并确保它不是空的。您需要检查empty,因为按照使用选择器的方式,空值最终会使选择器成为无效表达式,这就是为什么会出现无法识别的表达式错误

if(result.emp_presentEmployment !== undefined && result.emp_presentEmployment != ""){
    $('.prof_presentEmployed[value=' + result.emp_presentEmployment + ']').prop('checked', true).trigger("click");
}

如果(result.emp_presentEmployment){@anderidvoynos,您只需执行
,但如果合法值为
0
(错误值)该块将被跳过,因为它会将其视为false您是正确的,我猜我假设它将是属性上的字符串值在这种特定情况下可能是,但其他人可能会看到此答案,他们的值可能包括0或其他falsy类型的值,这就是为什么我这样编写它,以便两者都能受益:)