总是在ajax

时间:2016-10-06 07:20:17

标签: javascript jquery ajax

我想给出一个条件,如果id = type_investor的值为1或6,则使用不同的控制器发送邮件。

这是我的完整代码:

function (isConfirm) {
            if (!isConfirm) return;
            $.ajax({
                url: "{{URL::to("cpanel/deposit-withdraw-list/approve-depositwithdraw")}}",
                type: "POST",
                data: {id: fund_id,key: refdc_data},
                dataType: "html",
                success: function () {
                        $.ajax({
                            url: "{{URL::to("cpanel/deposit-withdraw-list/approve-tostatement")}}",
                            type: "POST",
                            data: {id: fund_id},
                            dataType: "html"});

                            if ($('#type_investor').val == 1){
                                $.ajax({
                                    url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-deposit")}}",
                                    type: "POST",
                                    dataType: "html"});
                            }else{
                                $.ajax({
                                    url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-withdraw")}}",
                                    type: "POST",
                                    dataType: "html"}); 
                            }

                    swal("Approved!", "It was succesfully Approved!", "success");
                    $('#refdc').val('');
                    window.location.href = "{{URL::to("cpanel/deposit-withdraw-list/")}}";
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    swal("Error Approved!", "Please try again", "error");
                    window.location.href = "{{URL::to("cpanel/deposit-withdraw-list/")}}";
                }
            });

我认为,问题的关键在于此代码

if ($('#type_investor').val == 1){
    $.ajax({
        url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-deposit")}}",
        type: "POST",
        dataType: "html"});
}else{
    $.ajax({
        url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-withdraw")}}",
        type: "POST",
        dataType: "html"}); 
}

因为如果id="type_investor"的条件值为1,则在 else 条件下使用控制器。 它始终使用 else 条件。我不知道实际上我的问题在哪里。

3 个答案:

答案 0 :(得分:2)

val是一个函数,您必须调用它:

if ($('#type_investor').val() == 1){
// ------------------------^^

请注意,您依赖于隐式转换,因为val始终返回一个字符串(除非jQuery集为空,然后返回undefined)。 FWIW,您可以找到各种选项,以隐式或明确地将字符串转换为my other answer here中的数字。

答案 1 :(得分:1)

您正在调用$('#type_investor').val,并返回值undefined

val是一个函数而不是属性

你应该把它称为

$('#type_investor').val();

答案 2 :(得分:0)

我找到了我的解决方案,在使用调试器之后,我使用了.text()。所以代码就像这样

if ($('#type_investor').text == 1){
$.ajax({
    url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-deposit")}}",
    type: "POST",
    dataType: "html"});
}else{
$.ajax({
    url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-withdraw")}}",
    type: "POST",
    dataType: "html"});
}