-2

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.

Slatyoo
  • 130
  • 15
arbong
  • 59
  • 12

3 Answers3

2

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.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I add () like that, and that doesn't work. I still get ELSE condition – arbong Oct 06 '16 at 07:26
  • @arbong: Then the `id="type_investor"` element doesn't exist, or doesn't have the value `"1"`. – T.J. Crowder Oct 06 '16 at 07:27
  • this value is exist and have value = "1" – arbong Oct 06 '16 at 07:29
  • @arbong if it is really exist then this is the right one. – Trafalgar D Law Oct 06 '16 at 07:31
  • @arbong: No, it doesn't. If it did, `$('#type_investor').val() == 1` would be `true`. [`select` isn't broken](https://pragprog.com/the-pragmatic-programmer/extracts/tips). Could you have more than one `id="type_investor"` elements? If so, usually the first one is picked (though since having multiple elements with the same `id` is invalid, that's not guaranteed and relies on the browser's underlying `getElementById` behavior). – T.J. Crowder Oct 06 '16 at 07:31
  • I used ctrl+f to find id="type_investor" . and there is only one in that page. So, this make me confuse why it doesn't work – arbong Oct 06 '16 at 07:37
1

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();
murli2308
  • 2,976
  • 4
  • 26
  • 47
0

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"});
}
arbong
  • 59
  • 12