# ［JSEdit for kintone使用］ドロップダウンの選択によって特定のレコードを非表示にしたいです。

**URL:** <https://community.cybozu.dev/t/topic/6202>\
**Category:** kintone 開発相談\
**Created:** [2022 年 7 月 29 日午前 9:32 UTC](https://community.cybozu.dev/t/topic/6202 "2022-07-29T09:32:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account2171](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Legacy\_Account2171](https://community.cybozu.dev/u/Legacy_Account2171)\
**Post date:** [2022 年 7 月 29 日午前 9:32 UTC](https://community.cybozu.dev/t/topic/6202/1 "2022-07-29T09:32:46Z")

</div>

kintone初学者です。初質問です。

現在顧客管理アプリを作成中です。

ドロップダウンのフィールド名：事業種別［個人・法人］で法人を選択した際に個人名のフィールドを非表示にしたいです。

各フィールドコードはそのままですが

事業種別→事業種別

法人名→法人名

個人名→個人名

となっております。

現在検索しながら下記コード入力しておりますが正しく表示されません。

(() =\> {  
&nbsp; ‘use strict’;

&nbsp; const events = [‘app.record.detail.show’,  
&nbsp; &nbsp; ‘app.record.create.show’,  
&nbsp; &nbsp; ‘app.record.create.change.事業種別’,  
&nbsp; &nbsp; ‘app.record.create.change.個人名’,  
&nbsp; &nbsp; ‘app.record.edit.show’,  
&nbsp; &nbsp; ‘app.record.edit.change.事業種別’,  
&nbsp; &nbsp; ‘app.record.edit.change.個人名’,];

&nbsp; kintone.events.on(events, (event) =\> {

&nbsp; &nbsp; const record = event.record;  
&nbsp; &nbsp;// lengthプロパティが違う気がするのですが・・・  
&nbsp; &nbsp; const 事業種別 = record.事業種別.value;  
&nbsp; &nbsp; if (事業種別.length === 法人) {  
&nbsp; &nbsp; &nbsp; kintone.app.record.setFieldShown(‘個人名’, false);  
&nbsp; &nbsp; }

&nbsp; });  
})();

わかりにくい文で申し訳ございません。

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1177](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@Legacy\_Account1177](https://community.cybozu.dev/u/Legacy_Account1177)\
**Post date:** [2022 年 7 月 30 日午前 12:27 UTC](https://community.cybozu.dev/t/topic/6202/2 "2022-07-30T00:27:49Z")

</div>

```
    const 事業種別 = record.事業種別.value;
    if (事業種別.length === 法人) {
      kintone.app.record.setFieldShown('個人名', false);
    }

```

ここを

```
  if (record['事業種別'].value === '法人') {
      kintone.app.record.setFieldShown('個人名', false);
  } else {
kintone.app.record.setFieldShown('個人名', true);
}

return event;

```

このように変更して下さい。changeイベントも含めるのであれば、条件を満たさない場合にフィールドを表示する処理も追加した方が良いでしょう（しないと一度法人を選択した後に他のものを選択しても表示されなくなります）。  
定数や変数に日本語を使うのはあまりおすすめできません。またlengthは長さを参照するものなので、提示されたコードだと事業種別の文字の長さを数えていることになります（また、ドロップダウンが何も選択されていない場合はlengthは参照できずエラーになります）。比較する「法人」は文字列なのでシングルクォーテーションまたはダブルクォーテーションで囲む必要があります。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2171](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Legacy\_Account2171](https://community.cybozu.dev/u/Legacy_Account2171)\
**Post date:** [2022 年 7 月 30 日午前 10:13 UTC](https://community.cybozu.dev/t/topic/6202/3 "2022-07-30T10:13:48Z")

</div>

hashimoto様

コメントありがとうございます！細かいところも教えてくださり感謝しています。まだまだだ勉強が足りないと痛感致しました。

下記のように変更してみましたがうまく動きませんでした。どこがダメなのかわからないですが調べてチャレンジしていきます！

(() =\> {  
&nbsp; ‘use strict’;

&nbsp; const events = [‘app.record.detail.show’,  
&nbsp; &nbsp; ‘app.record.create.show’,  
&nbsp; &nbsp; ‘app.record.create.change.事業種別’,  
&nbsp; &nbsp; ‘app.record.create.change.個人名’,  
&nbsp; &nbsp; ‘app.record.edit.show’,  
&nbsp; &nbsp; ‘app.record.edit.change.事業種別’,  
&nbsp; &nbsp; ‘app.record.edit.change.個人名’,];

&nbsp; kintone.events.on(events, (event) =\> {

&nbsp; &nbsp; if (record[‘事業種別’].value === ‘法人’) {  
&nbsp; &nbsp; &nbsp; kintone.app.record.setFieldShown(‘個人名’, false);  
&nbsp; &nbsp; } else {  
&nbsp; &nbsp; &nbsp; kintone.app.record.setFieldShown(‘個人名’, true);  
&nbsp; &nbsp; }

&nbsp; &nbsp; return event;

&nbsp; });  
})();

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1177](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@Legacy\_Account1177](https://community.cybozu.dev/u/Legacy_Account1177)\
**Post date:** [2022 年 7 月 30 日午後 11:16 UTC](https://community.cybozu.dev/t/topic/6202/4 "2022-07-30T23:16:44Z")

</div>

おもちさま

```
    const record = event.record;

```

こちらは残して下さい。または

```
if (event.record['事業種別'].value === '法人') { ...

```

というように記載しても問題ありません。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2171](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@Legacy\_Account2171](https://community.cybozu.dev/u/Legacy_Account2171)\
**Post date:** [2022 年 7 月 31 日午前 2:09 UTC](https://community.cybozu.dev/t/topic/6202/5 "2022-07-31T02:09:09Z")

</div>

hashimoto様

出来ました！これからからもっと勉強していっていろんなこと出来るように頑張ります！本当にありがとうございました！

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/cybozu/original/1X/d5c841c3c5a6cb66eed514001c1b45147c9e1f4f.png) [@system](https://community.cybozu.dev/u/system)\
**Post date:** [2024 年 9 月 6 日午前 12:53 UTC](https://community.cybozu.dev/t/topic/6202/6 "2024-09-06T00:53:50Z")

</div>

このトピックはベストアンサーに選ばれた返信から 3 日が経過したので自動的にクローズされました。新たに返信することはできません。
