【js】UncaughtTypeError:未定義のプロパティ「trim」を読み取れません



Js Uncaught Typeerror



覚えておく価値があります。

取得できるオブジェクトは未定義です。 [私のエラーの理由、$( '#XxxID')はID属性をDOMオブジェクトに追加するのを忘れていました。結果のオブジェクトは未定義です]



==========

参照:https://stackoverflow.com/questions/32733423/uncaught-typeerror-cannot-read-property-trim-of-undefined-in-jquery



コアコンテンツ:[英語の答えはまだ見る必要があります。ハハ]

You're getting error Uncaught TypeError: Cannot read property 'trim' of undefined in Jquery that means, the variable vname is undefined. To prevent this error from occurring, you can use the ternary operator to set the default value of the string to empty string when it is undefined. var vname = $('#EarningsTypes').val() == undefined ? '' : $('#EarningsTypes').val().trim() vname = vname.replace(/ /g, '%20') You can also use || to set the default value var vname = $('#EarningsTypes').val() || ''