简体   繁体   中英

always choose else condition in ajax

I want to give a condition if value of id=type_investor is 1 or 6 to send mail using different controller.

here is my full code :

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/")}}";
                }
            });

And I think, maybe the point of problem is in this code

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"}); 
}

because if condition value of id="type_investor" is 1 , it use controller in else condition. it always using else condition. I dont know where is actually my problem.

val is a function, you have to call it:

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

Note that you're relying on implicit conversion there, as val always returns a string (unless the jQuery set is empty, then it returns undefined ). FWIW, you can find your various options for implicitly or explicitly converting the string to a number in my other answer here .

You are calling $('#type_investor').val which returns the value undefined

val is a function not property

you should call it as

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

I found my solution, after use debugger, i used .text() . so the code is like this

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"});
}

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