LoginSignup
0
0

JavaScript(php smarty内 .tpl 記述) ラジオボタン、テキストエリア 制御

Posted at

■完成イメージ

・ラジオボックス(香炉)デフォルト、連絡事項に記述無し
・テキストエリア(連絡事項)
スクリーンショット (924).png

・ラジオボックス(香炉)香炉有りの場合は、連絡事項に追記
・テキストエリア(連絡事項)
スクリーンショット (925).png

※香炉以外の他にラジオボタンが3つ

■ソースコード

javascript main.js

// === コンテンツをロードしたら実行
document.addEventListener("DOMContentLoaded", function() {

    document.querySelectorAll('input[name="kouro_kubun"]').forEach(function(radio) {
        radio.addEventListener('change', updateTextarea);
    });

    document.querySelectorAll('input[name="death_weight_tmp"]').forEach(function(radio) {
        radio.addEventListener('change', update_Death_Wight);
    });

  
document.querySelectorAll('input[name="Machi_show"]').forEach(function(radio) {
        radio.addEventListener('change', update_Machi_show);
    });

    update_Death_Wight();

    update_Machi_show();

});

function updateTextarea() {
    var kouroKubunValue = document.querySelector('input[name="kouro_kubun"]:checked').value;
    var textarea = document.querySelector('textarea[name="stuff_info"]');
    var currentTextareaValue = textarea.value;

    currentTextareaValue = currentTextareaValue.replace(/香炉:香炉有り\n/g, '');

    if (kouroKubunValue === '1') {
        textarea.value = currentTextareaValue + '香炉:香炉有り\n';
        console.log("連絡事項:::" + textarea.value + "\n");
    } else {
        textarea.value = currentTextareaValue;
    }
}

function update_Death_Wight() {
    var death_weight_tmp_Value = document.querySelector('input[name="death_weight_tmp"]:checked').value;
    var textarea = document.querySelector('textarea[name="stuff_info"]');
    var currentTextareaValue = textarea.value;

    currentTextareaValue = currentTextareaValue.replace(/体重:100kg 以下\n|体重:100kg 以上\n/g, '');

    if (death_weight_tmp_Value === '0') {
        textarea.value = currentTextareaValue + '体重:100kg 以下\n';
        console.log("連絡事項:::" + textarea.value + "\n");
    } else if (death_weight_tmp_Value === '1') {
        textarea.value = currentTextareaValue + '体重:100kg 以上\n';
        console.log("連絡事項:::" + textarea.value + "\n");
    }
}

function update_Machi_show() {
    var Machi_show = document.querySelector('input[name="Machi_show"]:checked').value;
    var textarea = document.querySelector('textarea[name="stuff_info"]');
    var currentTextareaValue = textarea.value;

    currentTextareaValue = currentTextareaValue.replace(/待合室利用:小さい部屋\n|待合室利用:大きい部屋\n/g, '');

    if (Machi_show === '1') {
        textarea.value = currentTextareaValue + '待合室利用:小さい部屋\n';
        console.log("連絡事項:::" + textarea.value + "\n");
    } else if (Machi_show === '2') {
        textarea.value = currentTextareaValue + '待合室利用:大きい部屋\n';
        console.log("連絡事項:::" + textarea.value + "\n");
    } else {
        currentTextareaValue = currentTextareaValue.replace(/待合室利用:小さい部屋\n|待合室利用:大きい部屋\n/g, '');
        textarea.value = currentTextareaValue;
        console.log("連絡事項:::" + textarea.value + "\n");
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0