根据 jQuery 中的选择菜单创建的输入更改变量值
我需要使用 ajax 和 php 将输入值传递到数据库,但在将值存储在变量中以将其作为查询字符串传递时遇到问题,因为我不知道会有多少输入。所需的代码位于 $("#addVote").click(function(){ });
JSFiddle: http://jsfiddle.net/m55U4/
<form action="" method="post">
<div class="add_vote" style="width:300px; min-height:50px; max-height:100%;">
<div class="question" style="width:210px; float:right;">
<select id="Num">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div class="answers" style="width:300px; float:right; min-height:30px; max-height:100%;"></div>
<div class="add_vote" style="width:300px; float:right; min-height:30px; max-height:100%; text-align:center;">
<input name="" type="button" id="addVote" value="save" />
</div>
</div>
</form>
JavaScript:
$(document).ready(function () {
var num = $("#Num :selected").val();
get_num();
$(".answers").html('');
var num = $("#Num :selected").val();
for (var x = 1; x <= num; x++) {
$(".answers").append('<input type="text" class="' + x + '">');
}
$("#addVote").click(function () {
});
});
function get_num() {
$("#Num").change(function () {
$(".answers").html('');
var num = $("#Num :selected").val();
for (var x = 1; x <= num; x++) {
$(".answers").append('<input type="text" class="' + x + '">');
}
});
}
I need to pass input values to the db using ajax and php but have a problem storing values in a variable to pass it as a query string because I don't know how many inputs there will be. The needed code is inside $("#addVote").click(function(){ });
JSFiddle: http://jsfiddle.net/m55U4/
<form action="" method="post">
<div class="add_vote" style="width:300px; min-height:50px; max-height:100%;">
<div class="question" style="width:210px; float:right;">
<select id="Num">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div class="answers" style="width:300px; float:right; min-height:30px; max-height:100%;"></div>
<div class="add_vote" style="width:300px; float:right; min-height:30px; max-height:100%; text-align:center;">
<input name="" type="button" id="addVote" value="save" />
</div>
</div>
</form>
Javascript:
$(document).ready(function () {
var num = $("#Num :selected").val();
get_num();
$(".answers").html('');
var num = $("#Num :selected").val();
for (var x = 1; x <= num; x++) {
$(".answers").append('<input type="text" class="' + x + '">');
}
$("#addVote").click(function () {
});
});
function get_num() {
$("#Num").change(function () {
$(".answers").html('');
var num = $("#Num :selected").val();
for (var x = 1; x <= num; x++) {
$(".answers").append('<input type="text" class="' + x + '">');
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用方法 .serialize() 来序列化表单。
您必须为输入指定名称,以便该方法能够生成查询字符串。
演示
You can use the method .serialize() to serialize a form.
You would have to give names to your inputs in order for the method to be able to generate the querystring.
DEMO