【问题标题】:How do I get the value of the added input fields with Jquery?如何使用 Jquery 获取添加的输入字段的值?
【发布时间】:2020-04-06 20:41:15
【问题描述】:

我正在尝试从添加的输入字段中获取值并将其显示在文本区域中。

一旦点击codeBtn,输入字段的值将被添加,并将其添加到文本区域中。我只设法从用 HTML 创建的第一个输入字段中获取值。

此外,我已经尝试添加新的值输入字段。

let titleValue2 = $('#titleInput2').val();

let contentValue2 = $('#contentInput2').val();


totalString += (titleValue1 + contentValue1 + titleValue2 + contentValue2)

但是当我只想显示 titleInput1 和 contentInput1 的值时,这会给我“未定义”。 基本上我正在尝试从每个添加的输入字段中获取值到文本区域。 谁能帮帮我吗?谢谢

示例 HTML 代码:

<div class="btn-section">
            <button class="addButton">+</button>
            <button class="codeButton">Generate code</button>
        </div>

<div class="container">
    <label for="">Titel:</label>
    <input type="text" id="titleInput1">

    <label for="">Content:</label>
    <input type="text" id="contentInput1">
</div>
<div class="newInputs">

</div>
<textarea name="" id="textInput" cols="30" rows="10"></textarea>

JQuery 代码:

let inputCount = 1;
let totalString = ''
const defaultInput_firstpart = `<label for="">Titel:</label>
<input type="text" id="titleInput`
const defaultInput_secondpart = `">
<label for="">Content:</label>
<input type="text" id="contentInput`
const defaultInput_lastpart = `">`

function addNewInputs() {
    $('.newInputs').append(defaultInput_firstpart + inputCount + defaultInput_secondpart + inputCount + defaultInput_lastpart)
}

function addValues() {
    let titleValue1 = $('#titleInput1').val();
    let contentValue1 = $('#contentInput1').val()
    let titleValue2 = $('#titleInput2').val();
    let contentValue2 = $('#contentInput2').val()


    totalString += (titleValue1 + contentValue1 + titleValue2 + contentValue2)

}

$(document).ready(function() {

    $('.addButton').on('click', function() {
       inputCount++;
       addNewInputs();

    })

    $('.codeButton').on('click', function() {
        addValues();
        $('#textInput').text(totalString)

    })

})

【问题讨论】:

    标签: javascript jquery html input default-value


    【解决方案1】:

    而是使用动态创建的 id 使用类来获取输入值。 下面是例子

    let inputCount = 1;
    let totalString = ''
    const defaultInput_firstpart = `<div class="nwlist"><label for="">Titel:</label>
    <input type="text" class="titleInput" id="titleInput`
    const defaultInput_secondpart = `">
    <label for="">Content:</label>
    <input type="text" class="contentInput" id="contentInput`
    const defaultInput_lastpart = `"></div>`
    
    function addNewInputs() {
        $('.newInputs').append(defaultInput_firstpart + inputCount + defaultInput_secondpart + inputCount + defaultInput_lastpart)
    }
    
    function addValues() {
        totalString = "";
    	
    	$(".nwlist").each(function(){
    		totalString = totalString + $(this).find(".titleInput").val() + " " + $(this).find(".contentInput").val() + " ";
    	})
    }
    
    $(document).ready(function() {
        $('.addButton').on('click', function() {
           inputCount++;
           addNewInputs();
        })
        $('.codeButton').on('click', function() {
            addValues();
            $('#textInput').text(totalString)
        })
    })
    <!DOCTYPE html>
    <html>
    	<head>
    		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    		<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
    		<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	</head>
        <body>
            <div class="btn-section">
                <button class="addButton">+</button>
                <button class="codeButton">Generate code</button>
            </div>
    
    		<div class="container">
    			<div class="nwlist">
    				<label for="">Titel:</label>
    				<input type="text" class="titleInput" id="titleInput1">
    
    				<label for="">Content:</label>
    				<input type="text" class="contentInput" id="contentInput1">
    			</div>
    			<div class="newInputs">
    
    			</div>
    		</div>
    		<textarea name="" id="textInput" cols="30" rows="10"></textarea>
        </body>
    </html>    

    希望它会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 2018-02-21
      • 2014-12-26
      • 2012-12-21
      • 2022-11-25
      • 2020-02-15
      • 1970-01-01
      • 2021-02-14
      相关资源
      最近更新 更多