简体   繁体   中英

Getting value after added input box

i use a javascript to add a input box in my table as below:

var inputtopik = "topik" + 1;
$("#titleinput tbody").append("<tr><td> Topik " + topikno +
                              " : </td><td><input type='text' id='" +
                              inputtopik +"' style='WIDTH:498px;' ></td></tr>");

and then try to get the value by this

var topik = document.getElementById('inputtopik').value;

but got an error state

Uncaught TypeError: Cannot read property 'value' of null 

i wonder why? maybe it cannot find the input box that ive just add? how to fix? help :(

the value of inputtopik is "topik1". So you should do

var topik = document.getElementByID(inputtopik).value;

without the single quotes.

如果您使用的是jQuery ...获取输入文本值的另一种方法是:

var topik = $('#topik1').val();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM