爱好有3个,我写3个check控件
1.<asp:checkbox runat=“serve” ID=“book” text=“书籍”/>
2.<asp:checkbox runat=“serve” ID=“sport” text=“运动”/>
3.<asp:checkbox runat=“serve” ID=“game” text=“游戏”/>
想用sql=“insert into 表 where value()”来获取text里面的内容(就是我选书籍,数据库就会出现书籍,我选游戏,运动,数据库里面就会有游戏,运动)
如果只能选一个,题主应该用RadioButtonList控件,而不是checkbox,CheckBox是复选框可以多选。
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button1_Click(object sender, EventArgs e)
{
string value = "";
if (book.Checked) value =book.Text;
else if (sport.Checked) value =sport.Text;
else if (game.Checked) value =game.Text;
//radiobutton用下面的代码
//value = rb.SelectedValue;
string sql = "insert into 表(表中的字段)values('"+value+"')";
Response.Write(sql);
//执行sql语句的代码
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:RadioButtonList ID="rb" runat="server">
<asp:ListItem Value="书籍">书籍</asp:ListItem>
<asp:ListItem Value="运动">运动</asp:ListItem>
<asp:ListItem Value="游戏">游戏</asp:ListItem>
</asp:RadioButtonList>
<asp:checkbox runat="server" ID="book" text="书籍"/>
<asp:checkbox runat="server" ID="sport" text="运动"/>
<asp:checkbox runat="server" ID="game" text="游戏"/>
<asp:Button runat="server" Text="提交" OnClick="button1_Click" ID="button1" />
</form>
</body>
</html>
有帮助麻烦点下【采纳该答案】,谢谢~~有其他问题可以继续交流~
你好,
就像上面csdn专家说的那样, 你想要单选还是双选? 如果你想要的是单选,那我建议你是用radiobutton 比较好. 如果你想要选多个值,那么可以用checkbox. 但是在用的时候,你需要循环所有的checkbox 是否被选中, 然后再去获得所有checkbox 的值.
方法的话其实也有很多种,比如:第一种,使用使用csdn专家赖老师的方法, 在前端使用jquery 获取checkbox 的值,然后使用ajax 传到后台,sql query 中附上该参数.
第二种,使用csdn-showbo的方法.写一个checkbox 的changed 事件, 在后台判断checked是否为true.然后得到他们的值.
function getText()
{
var labels = document.getElementsByTagName("book");
var txt=labels[0].innerHTML;
alert(txt);
}