# ラジオボタンを選択すると、数値が入力される方法

**URL:** <https://community.cybozu.dev/t/topic/3111>\
**Category:** kintone 開発相談\
**Created:** [2018 年 6 月 4 日午前 5:50 UTC](https://community.cybozu.dev/t/topic/3111 "2018-06-04T05:50:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account717](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Legacy\_Account717](https://community.cybozu.dev/u/Legacy_Account717)\
**Post date:** [2018 年 6 月 4 日午前 5:50 UTC](https://community.cybozu.dev/t/topic/3111/1 "2018-06-04T05:50:41Z")

</div>

ラジオボタンで選択した数値をグラフに利用したいと考えています。

現状そのままでは、ラジオボタンで選択した数値は利用できないので、別フィールドに選択した数値を代入して活用できないかと考えています。

ここを参考にさせて頂き、

ラジオボタンＡ → 数値１

に代入は出来たのですが、

ラジオボタンＡ → 数値１

ラジオボタンＢ → 数値２

といった複数個になると、できなくなってしまいます。

基礎的なことかも分かりませんが、作り方を教えて頂けると助かります。

よろしくお願いします。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2036](https://avatars.discourse-cdn.com/v4/letter/l/a183cd/32.png) [@Legacy\_Account2036](https://community.cybozu.dev/u/Legacy_Account2036)\
**Post date:** [2018 年 6 月 6 日午前 3:49 UTC](https://community.cybozu.dev/t/topic/3111/2 "2018-06-06T03:49:28Z")

</div>

Hikaru Ishiharaさん

ラジオボタンに複数の選択肢があり(例：100、200、300)、

「ラジオボタンの選択によって数値フィールドの値を変化させたい」

ということでしょうか？

&nbsp;

であれば簡単なJavaScriptカスタマイズで実現できると思います。

(function() {  
&nbsp; &nbsp; ‘use strict’;

&nbsp; &nbsp; // とりあえずレコード追加イベントのみです  
&nbsp; &nbsp; var events = [  
&nbsp; &nbsp; &nbsp; &nbsp; ‘app.record.create.show’,  
&nbsp; &nbsp; &nbsp; &nbsp; ‘app.record.create.change.ラジオボタン’  
&nbsp; &nbsp; ];  
&nbsp; &nbsp; kintone.events.on(events, function(event) {

&nbsp; &nbsp; &nbsp; &nbsp; // ラジオボタンの値を取得して数値に変換する  
&nbsp; &nbsp; &nbsp; &nbsp; var radio\_val = Number(event.record.ラジオボタン.value);

&nbsp; &nbsp; &nbsp; &nbsp; // 数値フィールドへ挿入する  
&nbsp; &nbsp; &nbsp; &nbsp; event.record.数値.value = radio\_val;  
&nbsp; &nbsp; &nbsp; &nbsp; return event;  
&nbsp; &nbsp; });  
})();

---

<div class="post-metadata">

**Author:** ![Legacy\_Account717](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Legacy\_Account717](https://community.cybozu.dev/u/Legacy_Account717)\
**Post date:** [2018 年 6 月 6 日午前 5:03 UTC](https://community.cybozu.dev/t/topic/3111/3 "2018-06-06T05:03:12Z")

</div>

ありがとうございます。

説明下手ですいません。仰った事で間違いありません。

もう一つ質問になり恐縮ですが、

ラジオボタン１は数値１へ

ラジオボタン２は数値２へ

といった同じような行動を行いたい場合、以下の様に記せばよろしいのでしょうか？

コードが長くなってしまう為、分かりやすくしたいと思いますが、

もっと簡略化出来るのでしょうか？

&nbsp;

(function() {

‘use strict’;

&nbsp;

var events1 = [

‘app.record.create.show’,

‘app.record.create.change.a1’,

‘app.record.edit.show’,

‘app.record.edit.change.a1’

];

kintone.events.on(events1, function(event) {

// ラジオボタン\_a1の値を取得して数値に変換する

var radio\_a1 = Number(event.record.a1.value);

// 数値1へ挿入する

event.record.数値1.value = radio\_a1;

return event;

});

var events2 = [

‘app.record.create.show’,

‘app.record.create.change.a2’,

‘app.record.edit.show’,

‘app.record.edit.change.a2’

];

kintone.events.on(events2, function(event) {

// ラジオボタン\_a2の値を取得して数値に変換する

var radio\_a2 = Number(event.record.a2.value);

// 数値2へ挿入する

event.record.数値2.value = radio\_a2;

return event;

});

})();

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2036](https://avatars.discourse-cdn.com/v4/letter/l/a183cd/32.png) [@Legacy\_Account2036](https://community.cybozu.dev/u/Legacy_Account2036)\
**Post date:** [2018 年 6 月 6 日午前 5:32 UTC](https://community.cybozu.dev/t/topic/3111/4 "2018-06-06T05:32:32Z")

</div>

そうですね。イベント内の処理はまとめられるとは思いますが、あまり変化しないかもですね。

&nbsp;

他の方法としては、自分で先のコメントで記述していて恐縮ですがshowやchangeイベントを使わず、

submitイベントに限定するという方法もあるかと思います。

&nbsp;

submitイベントの場合、レコード保存ボタンを押したときのみ処理が走るようになるので、

レコード入力時は数値フィールドの値は変化しません。

ただ、そもそも数値自体はグラフで利用したいとのことなので、

レコード入力中にリアルタイムで数値が変化することはそれほど重要ではないかなとも思いました。

数値フィールドはグループフィールドの中へ格納して簡単には見えないようにすればレコード入力中も気にならないと思います。

&nbsp;

(function()&nbsp; {

&nbsp; &nbsp; ‘use strict’;

&nbsp; &nbsp; var events = [

&nbsp; &nbsp; &nbsp; &nbsp; ‘app.record.create.submit’,

&nbsp; &nbsp; &nbsp; &nbsp; ‘app.record.edit.submit’

&nbsp; &nbsp; ];

&nbsp; &nbsp; kintone.events.on(events, function(event)&nbsp; {

&nbsp; &nbsp; &nbsp; &nbsp; // ラジオボタンの値を取得して数値に変換する

&nbsp; &nbsp; &nbsp; &nbsp; var radio\_a1 = Number(event.record.a1.value);

&nbsp; &nbsp; &nbsp; &nbsp; var radio\_a2 = Number(event.record.a2.value);

&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; // 数値へ挿入する

&nbsp; &nbsp; &nbsp; &nbsp; event.record.数値1.value = radio\_a1;

&nbsp; &nbsp; &nbsp; &nbsp; event.record.数値2.value = radio\_a2;

&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; return event;

&nbsp; &nbsp; });

})();

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account717](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Legacy\_Account717](https://community.cybozu.dev/u/Legacy_Account717)\
**Post date:** [2018 年 7 月 2 日午前 5:20 UTC](https://community.cybozu.dev/t/topic/3111/5 "2018-07-02T05:20:27Z")

</div>

返信が大変遅くなってしまい申し訳ございません。

こちらの手違いで返事を書けていないことに気付くのが遅くなってしまいました。

おかげさまで問題は解決できました。ありがとうございます。

今後ともよろしくお願いいたします。
