jQuery - 在文本模板中使用用户输入值 - 最佳实践?

时间:2018-01-22 02:29:32

标签: jquery text input

我有一个表单,用户输入几十个值来生成报告。所需的文本输出具有特定的格式,因此为了生成它,我一直这样做:

Application.VlookUp

等。有没有更有效的方法来做到这一点,以便它更具可读性?  考虑到输入字段的数量并且有点笨拙,目前这个文本块是几百行。

1 个答案:

答案 0 :(得分:0)

for循环 ...

怎么样?



$(document).ready(function() {

console.log('1. Document is ready.');

// Run the App.
runApplication();

});

// We create a variable called runApplication and assign it a function runApplication()
var runApplication = function runApplication() {

console.log('1.1 runApplication() function called.');

// We make a reference to all element inputs of type text inside form1..
var elements = document.querySelectorAll('#form1 input[type=text]');

// then we create a Global string variable to append stuff to...
var final_string = '';

// then for each and every punt of type text found inside form1...
for (var i = 0; i < elements.length; i++) {
    // if it's really somthing other than nothing....
    if(elements[i] = true) {
        // append current input text value in the lopp to variable final_string
        final_string = final_string + elements[i].value + ' ';
        console.log(elements[i].value);
    }
}

// output final_string with all input text values...
// ..joined together by the for loop.
console.log(final_string);

};
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1">
<input type="text" value="1">
<input type="text" value="2">
<input type="text" value="3">
<input type="text" value="4">
<input type="text" value="5">
</form>
&#13;
&#13;
&#13;