gpt4 book ai didi

javascript - 如何将使用 javascript 和 ASP.NET 生成的字段中的数据发送到 Microsoft SQL?

转载 作者:行者123 更新时间:2023-12-03 01:56:02 28 4
gpt4 key购买 nike

我已经使用 Javascript 和 ASP.NET 创建了一个代码来生成调查所需的字段,我想知道如何将字段内的文本发送到数据库中。这是我使用 ASP.NET 生成表的代码。我的目标是创建一个像 google form 这样的网络应用程序,可以让非程序员创建调查。

<div class="form-group" style="margin:auto; width:80%;">
<form name="add_question" id="add_question">
<asp:Label ID="Label1" runat="server" Text="Survey Title: " Font-Size="Large"></asp:Label>
<input type="text" name="title" id="title" class="form-control question_list" placeholder="Enter Survey Title"/>
<asp:Label ID="Label2" runat="server" Text="Description: " Font-Size="Large"></asp:Label>
<br>
<textarea rows="4" style="margin:auto; width:100%; resize: none;" name="description" id="description" placeholder="Enter description"></textarea>
<table class="table" id="dynamic_field">
<thead>
<tr>
<th style="width: 50%">Question</th>
<th style="width: 15%">Type of Question</th>
<th style="width: 32%">Fields</th>
<th style="width: 3%">Remove</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</form>
<button type="button" name="add" id="add" class="btn btn-warning" > Add Question </button>
<asp:Button name="submit" id="submit" class="btn btn-success" runat="server" Text="Submit" OnClick="submit_Click" />
</div>

下面是在表中创建新行并根据所选问题类型生成字段的 javascript 代码。

//ADD TABLE ROWS
$("#add").click(function () {
i++;
$("#dynamic_field tbody").append('<tr id="row' + i + '"> <td> <input type="text" name="question' + i + '" id="question'
+ i + '" class="form-control question_list" placeholder="Enter question"/> </td><td><select name="type' + i + '" id="' + i + '" class="dropdown">'
+ '<option selected></option>'
+ '<option value="1">Multiple Choice</option>'
+ '<option value="2">CheckBox</option>'
+ '<option value="3">Short Sentence</option>'
+ '<option value="4">Paragraph</option>'
+ '<option value="5">Line Scale</option>'
+ '</select></td><td></td><td><button name="remove' + i + '" id="'
+ i + '" class="btn btn-danger btn_remove"> X </button> </td> </tr> ');
});
$(document).on('change', '.dropdown', function () {
var xid = $(this).attr("id");
if ($(this).val() == '3') {
$('#myInput' + xid + '').remove();
var row = document.getElementById("row"+xid+"");
var z = row.insertCell(2);
z.innerHTML = '<td style="width: 32%"><input name="3" id="myInput' + xid + '" type="text" style="width: 100%" /></td>';
row.deleteCell(3);
}
else if ($(this).val() == '4') {
$('#myInput' + xid + '').remove();
var row = document.getElementById("row"+xid+"");
var z = row.insertCell(2);
z.innerHTML = '<td style="width: 32%"><textarea rows="2" cols="40" name="4" id="myInput' + xid + '" style="margin:auto; width:100%; resize: none;" ></textarea></td>';
row.deleteCell(3);
}
});

最佳答案

由于您是在客户端创建控件,因此服务器不会知道它们。在这种情况下,由于您使用 ASP.Net 并且有一个提交按钮,因此文本框和下拉列表中的值确实会在 Request.Forms 对象中发送回服务器。例如,如果您有一个名为 question1 的文本框,在提交按钮的单击事件处理程序上,您可以尝试如下操作:

Request.Forms["question1"]

这将为您提供文本框中的值,该值将发送回服务器。

请注意,还有其他方法可以处理这种动态场景,您可能想看看。

关于javascript - 如何将使用 javascript 和 ASP.NET 生成的字段中的数据发送到 Microsoft SQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50227166/

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