gpt4 book ai didi

javascript - 如何将动态创建的文本框值传递给jquery中的数组

转载 作者:行者123 更新时间:2023-11-28 19:10:32 25 4
gpt4 key购买 nike

我正在制作一个程序,我想在其中添加动态文本框作为最终结果。我想在最终结果框中添加所有第三个框的值,如果单击删除,则应从最终结果中扣除相应的值...

这里是代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<ol id="boxes">
</ol>
<input type="button" value="Add a row" id="add_boxes" />
<br> final result of all 3rd boxes:
<input type="text" value="" id="final_result" />
<script>
var add = $('#add_boxes');
var all = $('#boxes');
var amountOfInputs = 2;
var maximumBoxes = 10;

add.click(function(event) {

// create a limit
if ($(".box").length >= maximumBoxes) {
alert("You cannot have more than 10 boxes!");
return;
}

var listItem = $('<li class="box"></li>');
// we will add 2 boxes here, but we can modify this in the amountOfBoxes value
for (var i = 0; i < amountOfInputs; i++) {
listItem.append('<input type="text" class="input" />');
}
listItem.append('<input type="text" class="output" name="value" placeholder="3rd box"/>');
// Lets add a link to remove this group as well, with a removeGroup class
listItem.append('<input type="button" value="Remove" class="removeGroup" />')
listItem.appendTo(all);
});

// This will tie in ANY input you add to the page. I have added them with the class `input`, but you can use any class you want, as long as you target it correctly.
$(document).on("keyup", "input.input", function(event) {
// Get the group
var group = $(this).parent();
// Get the children (all that arent the .output input)
var children = group.children("input:not(.output)");
// Get the input where you want to print the output
var output = group.children(".output");
// Set a value
var value = 0;
// Here we will run through every input and add its value
children.each(function() {
// Add the value of every box. If parseInt fails, add 0.
value += parseInt(this.value) || 0;
var a = parseInt(value);
});
// Print the output value
output.val(value);
document.getElementById('final_result').value = value;



var test = document.getElementById('final_result').value
var b = parseInt(test) + parseInt(a);
alert(a);

});

// Lets implement your remove field option by removing the groups parent div on click
$(document).on("click", ".removeGroup", function(event) {
event.preventDefault();
$(this).parent(".box").remove();
});
</script>

最佳答案

$('.clickMe').on('click', function() {

var total=0;
var myInput1Vals = $("#boxes").find('.output').map(function() {
total += parseInt(this.value)
console.log("total : "+total);

return this.value;

});

$("#final_result").val(total);
});

添加数字的新功能

 function addNum(){
var total=0;
var myInput1Vals = $("#boxes").find('.output').map(function() {
total += parseInt(this.value)
console.log("total : "+total);

return this.value;

});
$("#final_result").val(total);
}

第三个盒子上的键盘功能

$('#boxes').on('keyup','.output', function() {
addNum();
});

FIDDLE DEMO

关于javascript - 如何将动态创建的文本框值传递给jquery中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30797000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com