JS 動態建立 input text 與其他input 的方法

  • 799
  • 0
  • JS
  • 2019-11-13

JS 動態建立 input text的方法

第一種 手動建

缺點: 又額外也ID 還要判斷說要加到哪個元素

function createRadioElement(name, checked) {
    var radioHtml = '<input type="radio" name="' + name + '"';
    if ( checked ) {
        radioHtml += ' checked="checked"';
    }
    radioHtml += '/>';

    var radioFragment = document.createElement('div');
    radioFragment.innerHTML = radioHtml;

    return radioFragment.firstChild;
}

https://stackoverflow.com/questions/118693/how-do-you-dynamically-create-a-radio-button-in-javascript-that-works-in-all-bro

在table動態建立 tr

https://www.tutorialrepublic.com/faq/how-to-add-remove-table-rows-dynamically-using-jquery.php

第二種

利用strinformat 建立物件  字串看起來會比較清晰

JavaScript – String.format

http://kevintsengtw.blogspot.com/2011/09/javascript-stringformat.html

 

以上文章僅用紀錄資料使用.....