gpt4 book ai didi

javascript - 添加单选按钮

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

如何使用jquery或javascript附加单选按钮以保存到数据库中?

我附加了输入类型,如文本、文本区域、复选框、带选项选择。

我的代码是

 <!DOCTYPE html>
<html lang="en">
<head>
<title>
apnd
</title>
<script src="jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var htmldata='<div>Name<input type="text" name="cname[]">Mobile Number<input type="text"name="mob[]">Gender<input type="radio" name="gender[]" value="M">Male<input type="radio" name="gender[]" value="F">FemaleAddress<textarea name="address[]"></textarea><input type="button" class="remove_field" value="remove"></div>';
$('.add_field').click(function(){
$('.wrapper').append(htmldata);
});
$('.wrapper').on('click','.remove_field',function(){
$(this).parent('div').remove();
});
});
</script>
</head>
<body>
<div class="container">
<form method="post" action="">
<div class="wrapper">
<div>
Name<input type="text" name="cname[]">
Mobile Number<input type="text" name="mob[]">
Gender<input type="radio" name="gender[]" value="M">Male
<input type="radio" name="gender[]" value="F">Female
Address<textarea name="address[]"></textarea>
<input type="button" class="add_field" value="Add">
</div>
</div>
<input type="submit" name="sub" value="Save">
</form>
</body>
</html>

使用 php 我想将值保存到数据库。

$name=sc_sql_injection($_POST['cname']);
$mobile=sc_sql_injection($_POST['mob']);
$gender=sc_sql_injection($_POST['gender']);
$address=sc_sql_injection($_POST['address']);
$count=count($_POST['cname']);
for($i=0;$i<$count;$i++)
{
$sql="insert into t2(name,mobile,gender,address) values('$name[$i]','$mobile[$i]','$gender[$i]','$address[$i]')";
sc_exec_sql($sql,$con);
}

更新 enter image description here

最佳答案

附加单选按钮

<!DOCTYPE html>
<html lang="en">
<head>
<title>
apnd
</title>
<script src="jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var i=1;
$('.add_field').click(function(){
var htmldata='<div>Name<input type="text" name="cname[]">Mobile Number<input type="text"name="mob[]"> Gender<input type="radio" name="gender['+i+']" value="M">Male<input type="radio" name="gender['+i+']" value="F">Female Address<textarea name="address[]"></textarea><input type="button" class="remove_field" value="remove"></div>';
i++;
$('.wrapper').append(htmldata);
});
$('.wrapper').on('click','.remove_field',function(){
$(this).parent('div').remove();
i--;
});
});
</script>
</head>
<body>
<div class="container">
<form method="post" action="">
<div class="wrapper">
<div>
Name<input type="text" name="cname[]">
Mobile Number<input type="text" name="mob[]">
Gender<input type="radio" name="gender[0]" value="M">Male
<input type="radio" name="gender[0]" value="F">Female
Address<textarea name="address[]"></textarea>
<input type="button" class="add_field" value="Add">
</div>
</div>
<input type="submit" name="sub" value="Save">
</form>
</body>
</html>

$(document).ready(function(){
var i=1;
$('.add_field').click(function(){
var htmldata='<div>Name<input type="text" name="cname[]">Mobile Number<input type="text"name="mob[]"> Gender<input type="radio" name="gender['+i+']" value="M">Male<input type="radio" name="gender['+i+']" value="F">Female Address<textarea name="address[]"></textarea><input type="button" class="remove_field" value="remove"></div>';
i++;
$('.wrapper').append(htmldata);
});
$('.wrapper').on('click','.remove_field',function(){
$(this).parent('div').remove();
i--;
});
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<form method="post" action="">
<div class="wrapper">
<div>
Name<input type="text" name="cname[]">
Mobile Number<input type="text" name="mob[]">
Gender<input type="radio" name="gender[0]" value="M">Male
<input type="radio" name="gender[0]" value="F">Female
Address<textarea name="address[]"></textarea>
<input type="button" class="add_field" value="Add">
</div>
</div>
<input type="submit" name="sub" value="Save">
</form>
</body>
</html>

关于javascript - 添加单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38959423/

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