質問をすることでしか得られない、回答やアドバイスがある。

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

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

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

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

Q&A

解決済

3回答

646閲覧

料金表の合計がマイナスの金額の場合、非表示にしたい

japanese_monkey

総合スコア0

JavaScript

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

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

0グッド

0クリップ

投稿2020/08/29 08:27

自動計算で料金額の設定を作ってるものですが、#field_total2と#field_total3 が マイナスの金額の場合、非表示にしたいです。プラスの金額のときのみの表示の仕方が分かりません。
よろしくお願いします。

<script> window.addEventListener('DOMContentLoaded', function(){ keisan(); document.querySelector('[name=goods1]').addEventListener('input', function(){ keisan(); }); document.querySelector('[name=goods2]').addEventListener('input', function(){ keisan(); }); document.querySelector('[name=goods3]').addEventListener('input', function(){ keisan(); }); document.querySelector('[name=goods4]').addEventListener('input', function(){ keisan(); }); }); function keisan(){ var price1 = parseInt(document.querySelector('[name=goods1]').value) * 1.5625; var price2 = parseInt(document.querySelector('[name=goods2]').value) * 1; var price3 = parseInt(document.querySelector('[name=goods3]').value) * 1; var price4 = parseInt(document.querySelector('[name=goods4]').value) * 1; var total1 = ((price1 / 8 * price2 * 3600 * price3 * price4) / 1024) / 1024 ; var tax2 = Math.floor(total1); var total2 = tax2 - 1536 ; var total3 = total2 * 20 ; document.querySelector('#field1').textContent=price1.toLocaleString(); document.querySelector('#field2').textContent=price2.toLocaleString(); document.querySelector('#field3').textContent=price3.toLocaleString(); document.querySelector('#field4').textContent=price4.toLocaleString(); document.querySelector('#field_total1').textContent=tax2.toLocaleString(); document.querySelector('#field_total2').textContent=total2.toLocaleString(); document.querySelector('#field_total3').textContent=total3.toLocaleString(); } </script>

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

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

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

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

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

guest

回答3

0

if(total2<0){
document.getElementById('field_total2');
}else{
document.querySelector('#field_total2').textContent=total2.toLocaleString();
}

if(total3<0){ document.getElementById('field_total3'); }else{ document.querySelector('#field_total3').textContent=total3.toLocaleString(); }

}

こちらで解決しました。

投稿2020/09/02 15:29

japanese_monkey

総合スコア0

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

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

0

自己解決

if(total2<0){
document.getElementById('field_total2');
}else{
document.querySelector('#field_total2').textContent=total2.toLocaleString();
}

if(total3<0){ document.getElementById('field_total3'); }else{ document.querySelector('#field_total3').textContent=total3.toLocaleString(); }

これでマイナスの金額が非表示になるようです。解決しました。

投稿2020/09/02 03:01

japanese_monkey

総合スコア0

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

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

0

JS

1window.addEventListener('DOMContentLoaded', function(){ 2 keisan(); 3 document.querySelector('[name=goods1]').addEventListener('input', function(){ 4 keisan(); 5 }); 6 document.querySelector('[name=goods2]').addEventListener('input', function(){ 7 keisan(); 8 }); 9 document.querySelector('[name=goods3]').addEventListener('input', function(){ 10 keisan(); 11 }); 12 document.querySelector('[name=goods4]').addEventListener('input', function(){ 13 keisan(); 14 }); 15}); 16 17function keisan(){ 18 var price1 = parseInt(document.querySelector('[name=goods1]').value) * 1.5625; 19 var price2 = parseInt(document.querySelector('[name=goods2]').value) * 1; 20 var price3 = parseInt(document.querySelector('[name=goods3]').value) * 1; 21 var price4 = parseInt(document.querySelector('[name=goods4]').value) * 1; 22 var total1 = ((price1 / 8 * price2 * 3600 * price3 * price4) / 1024) / 1024 ; 23 var tax2 = Math.floor(total1); 24 var total2 = tax2 - 1536 ; 25 var total3 = total2 * 20 ; 26 document.querySelector('#field1').textContent=price1.toLocaleString(); 27 document.querySelector('#field2').textContent=price2.toLocaleString(); 28 document.querySelector('#field3').textContent=price3.toLocaleString(); 29 document.querySelector('#field4').textContent=price4.toLocaleString(); 30 document.querySelector('#field_total1').textContent=tax2.toLocaleString(); 31 32 if(total2.toLocaleString()<0){ 33 document.getElementById('field_total2').style.display = 'none'; 34 }else{ 35 document.querySelector('#field_total2').textContent=total2.toLocaleString(); 36 } 37 38 if(total3.toLocaleString()<0){ 39 document.getElementById('field_total3').style.display = 'none'; 40 }else{ 41 document.querySelector('#field_total3').textContent=total3.toLocaleString(); 42 } 43}

投稿2020/08/29 19:24

kyoya0819

総合スコア10429

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

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

japanese_monkey

2020/08/30 03:53

回答をいただき、まことにありがとうございます。でもいまだにマイナスの金額で表示されてしまいます。 goods1のinput typeのvalue値が"800"でgoods2とgoods3とgoods4は"0"に設定しております。 #field_total2と#field_total3は<span>内で表示させております。そこに問題があるのでしょうか? 分かる人がいれば、教えていただけると、助かります。よろしくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問