Getting error when using .Val () in jquery

I am using jQuery and I have a sample jQuery code.

JQuery code:

 $.ajax({  
        type: "POST",
        url: "Login.aspx",  // Send the login info to this page
        data: str,  
        success: function(result)
        {  
             // Show 'Submit' Button
            $('#loginButton').show();

            // Hide Gif Spinning Rotator
            $('#ajaxloading').hide();  

            var resLength = (result).val().trim().length;
            alert(resLength);
            if(resLength!=0)
            {

                var arr = result.split(",");
                var fname = arr[0];
                var lname = arr[1];
                var activeCardNo = arr[2];
                var multipleTier = arr[3];
                var activeStatus = arr[4];
                var access = arr[5];
            }
        }
    }); 

In the above code example, when I try to use .val () in the line below

var resLength = (result).val().trim().length;

it gives the error "result.val is not a function" . If I use only result.trim (). length , it works fine in firefox, however giving an error in IE.

Please suggest!

+3
source share
5 answers

try the following:

var resLength = $.trim(result).length;

If resultis a string, it has no function val. trimCross-browser is not supported, so instead jQuery.trim.

. JavaScript, :

result = $.trim(result);
if(result)
{
    // split, ...
}

result , jQuery:

var resLength = $.trim($(result).val()).length;
+6

result.trim().length, IE trim() (. .trim() JavaScript, IE), .

, .

0

: -

var resLength = result.val().trim().length;
alert(resLength);

"(result)" "result" . , "result".

, .

0

(result).val();

html

html,

.text(); or .html()
0

jQuery, ajax

0

Source: https://habr.com/ru/post/1781187/


All Articles