\r\n```\r\n\r\n今回は、string型になるかと思います。\r\nstring型になるinputのtypeは、\r\n- text\r\n- tel\r\n- url\r\n- password\r\n\r\nなどです。\r\nstring型が多いです。","dateModified":"2019-05-29T06:58:53.748Z","datePublished":"2019-05-29T06:56:31.938Z","upvoteCount":2,"url":"https://teratail.com/questions/192000#reply-284912"},"suggestedAnswer":[{"@type":"Answer","text":"数値しかあつかえないnumberであってもvalueの型はstringです\r\n```javascript\r\n\r\n\r\n```","dateModified":"2019-05-29T06:47:39.465Z","datePublished":"2019-05-29T06:47:39.465Z","upvoteCount":4,"url":"https://teratail.com/questions/192000#reply-284909","comment":[{"@type":"Comment","text":"yambejp様\r\n返信ありがとうございます。\r\n\r\njavascriptは変数の型が分かりにくいですね。\r\n今後も勉強に励みます。","datePublished":"2019-05-31T10:32:12.636Z","dateModified":"2019-05-31T10:32:12.636Z"}]},{"@type":"Answer","text":"数値型か文字列型かの判定であれば`typeof`を使うと判定できます。\r\n\r\n意図的に変換してから使いたいのであればNumber()やString()などを使えば良いでしょう。","dateModified":"2019-05-29T06:44:59.539Z","datePublished":"2019-05-29T06:44:59.539Z","upvoteCount":5,"url":"https://teratail.com/questions/192000#reply-284906","comment":[{"@type":"Comment","text":"dice142様\r\n返信ありがとうございます。\r\n\r\n具体的な解決方法を教えていただき、ありがとうございます。","datePublished":"2019-05-31T10:35:10.282Z","dateModified":"2019-05-31T10:35:10.282Z"}]},{"@type":"Answer","text":"実際にやってみれば明白ですが\r\n```JavaScript\r\nconsole.log(typeof paymentSelect.value); // string\r\nlet heightInput = document.getElementById(\"heightInput\");\r\nconsole.log(typeof heightInput.value); // string\r\n```\r\n[https://developer.mozilla.org/ja/docs/Web/API/HTMLInputElement](https://developer.mozilla.org/ja/docs/Web/API/HTMLInputElement)\r\n[https://html.spec.whatwg.org/multipage/input.html#htmlinputelement](https://html.spec.whatwg.org/multipage/input.html#htmlinputelement)\r\n\r\n[https://developer.mozilla.org/ja/docs/Web/API/HTMLSelectElement](https://developer.mozilla.org/ja/docs/Web/API/HTMLSelectElement)\r\n[https://html.spec.whatwg.org/multipage/form-elements.html#htmlselectelement](https://html.spec.whatwg.org/multipage/form-elements.html#htmlselectelement)","dateModified":"2019-05-29T06:41:44.633Z","datePublished":"2019-05-29T06:41:44.633Z","upvoteCount":6,"url":"https://teratail.com/questions/192000#reply-284905","comment":[{"@type":"Comment","text":"x_x様、今回も解説ありがとうございます。\r\nコンソールと「typeof」を使えば良かったのですね。\r\n具体的な解決方法を示して下さり、ありがとうございます。","datePublished":"2019-05-31T10:39:27.735Z","dateModified":"2019-05-31T10:39:27.735Z"}]}],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/JavaScript","name":"JavaScriptに関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/192000","name":"javascript input.valueの型の種類に対する質問"}}]}}}
質問するログイン新規登録

Q&A

解決済

4回答

4087閲覧

javascript input.valueの型の種類に対する質問

taka_oct092018

総合スコア148

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2019/05/29 06:27

編集2019/05/31 10:25

0

0

input要素のvalueは数値型か文字列型なのかがよく分かりません。

(html) <select id="paymentSelect" name="paymentSelect"> <option value="0">選択してください</option> <option value="1">銀行振込</option> <option value="2">クレジットカード</option> </select>

paymentSelect.value:文字列型

(html) <input type="text" id="heightInput" name="height" placeholder="身長"> (javascript) let heightInput = document.getElementById("heightInput");

heightInput.value:数値型?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

miyabi_takatsuk

2019/05/29 06:39

コードは、<code>ボタンで挿入される、``` ```の間に記載ください。 ひとまず回答させていただきます。
guest

回答4

0

実際にやってみれば明白ですが

JavaScript

1console.log(typeof paymentSelect.value); // string 2let heightInput = document.getElementById("heightInput"); 3console.log(typeof heightInput.value); // string

https://developer.mozilla.org/ja/docs/Web/API/HTMLInputElement
https://html.spec.whatwg.org/multipage/input.html#htmlinputelement

https://developer.mozilla.org/ja/docs/Web/API/HTMLSelectElement
https://html.spec.whatwg.org/multipage/form-elements.html#htmlselectelement

投稿2019/05/29 06:41

x_x

総合スコア13749

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

taka_oct092018

2019/05/31 10:39

x_x様、今回も解説ありがとうございます。 コンソールと「typeof」を使えば良かったのですね。 具体的な解決方法を示して下さり、ありがとうございます。
guest

0

数値型か文字列型かの判定であればtypeofを使うと判定できます。

意図的に変換してから使いたいのであればNumber()やString()などを使えば良いでしょう。

投稿2019/05/29 06:44

dice142

総合スコア5158

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

taka_oct092018

2019/05/31 10:35

dice142様 返信ありがとうございます。 具体的な解決方法を教えていただき、ありがとうございます。
guest

0

数値しかあつかえないnumberであってもvalueの型はstringです

javascript

1<script> 2window.addEventListener('DOMContentLoaded', function(e){ 3 document.querySelector('#num').addEventListener('change',function(e){ 4 console.log(typeof e.target.value); 5 }); 6}); 7</script> 8<input type="number" id="num">

投稿2019/05/29 06:47

yambejp

総合スコア118164

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

taka_oct092018

2019/05/31 10:32

yambejp様 返信ありがとうございます。 javascriptは変数の型が分かりにくいですね。 今後も勉強に励みます。
guest

0

ベストアンサー

JavaScriptにおいて、型を確認するには、typeofキーワードを使えば行えるので、それで確認されるといいでしょう。

html

1<input type="text" id="heightInput" name="height" placeholder="身長"> 2 3<script> 4let heightInput = document.getElementById("heightInput"); 5console.log(typeof heightInput.value); 6</script>

今回は、string型になるかと思います。
string型になるinputのtypeは、

  • text
  • tel
  • url
  • password

などです。
string型が多いです。

投稿2019/05/29 06:56

編集2019/05/29 06:58
miyabi_takatsuk

総合スコア9572

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

taka_oct092018

2019/05/31 10:29

miyabi_takatsuk様 返信ありがとうございます。 初学者ゆえ「typeof」の使い方が分かっていませんでした。 具体例も上げていただき、より理解が深まりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.29%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問