gpt4 book ai didi

javascript - 用户如何从 html 表单中收集 Google 应用程序脚本中变量输入值的值?

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

我正在使用添加更多功能创建输入字段,然后将其发送到谷歌脚本文件,如何收集字段的值,如下面我实现的代码所示。

Google 应用脚本文件

代码.gs

function getFormValue(formValue) {
var myarr= {};
var count = formValue.count;
for(var g = 1; g<=count; g++ )
{
user["I"+g] = formValue.user+g; // error, what to do here
}
// code

}

index.html

    <script>
$(document).ready(function() {
var counter = 2;
$("#addMoreUser").click(function() {
if (counter > 7) {
alert("Only 7 Users are allowed");
return false;
}
var newRowDiv = $(document.createElement('div'))
.attr("id", 'rowDiv' + counter);
newRowDiv.after().html('<div class="row" id="rowDiv" ><div class="col-md-3"><input class="form-control" placeholder="user'+ counter +' " name="user'+ counter +'" id="user'+ counter +'" type="text" value=""></div></div>');
newRowDiv.appendTo("#rowDivGroup");
$("#count").val(counter);
counter++;
});

$( "#submitForm" ).submit(function() {
google.script.run.withSuccessHandler(function(ret){
console.log(ret);
}).getFormValue(this); //"this" is the form element
});
});

</script

<form class="contact-form" id="myform">
<input type="hidden" value="1" name="count" id="count">
<div id="rowDivGroup">
<div class="row" id="rowDiv">
<div class="col-md-3">
<input class="form-control" placeholder="Name of User" name="user1" id="user1" type="text" value=""></div></div></div>
<a class="btn btn-sm btn-flat btn-success btn-rounded" id="addMoreUser">Add More Users</a>
<input type="submit" class="btn btn-flat flat-color btn-rounded btn-sm" id="submitForm" value="Submit Details ">
</form>

最佳答案

此代码获取数组的值,并创建一个对象:

function getFormValue(formValue) {
formValue = ["someInput1", "someInput2", "someInput3", "someInput4", "someInput5", "someInput6", "someInput7"];
Logger.log('formValue: ' + formValue);

var myarr= {};
var count = formValue.length;

Logger.log('count: ' + count);

var user = {};

for(var g = 1; g<=count; g++ )
{
Logger.log('g: ' + g);
var k = "user"+g;

var userID = "I" + g;
Logger.log("userID: " + userID);
Logger.log("formValue: " + formValue[g-1]);

user[userID] = formValue[g-1];
Logger.log("The property value: " + user[userID]);
}

}

我已经运行了代码,它有效。出于测试目的,对数组的值进行了硬编码。

关于javascript - 用户如何从 html 表单中收集 Google 应用程序脚本中变量输入值的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27026071/

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