我想创建一个带有一些文本框的简单表单以及五星评级功能(http://rateit.codeplex.com/)。当点击提交按钮时,字段中的数据应该被添加到数据库中。从jQuery/JavaScript/AJAX检索值添加到数据库(ASP.NET/C#)
该文本框不是一个问题,但我无法从星级评分中“检索”该值。
我尝试了一种解决方法,即一旦选择了评分,就将值输出到标签中,但是,在写入数据库时,不会发送任何值。
HTML: 您的评价:
<div id="products">
<div style="float: right; width: 350px; border: 1px solid #ccc; padding: 1em;">
<strong>Server response:</strong>
<ul id="response">
</ul>
</div>
<div data-productid="653" class="rateit"></div>
</div>
<asp:Label ID="lblRating" runat="server" Text=""></asp:Label>
<br />
What was the best aspect of your experience?
<br />
<asp:TextBox ID="txtBest" TextMode="multiline" runat="server" Height="105px" Width="301px"></asp:TextBox>
<br />
What was the worst aspect of your experience?
<br />
<asp:TextBox ID="txtWorse" TextMode="multiline" runat="server" Height="105px" Width="301px"></asp:TextBox>
<br />
Do you have any suggestions?
<br />
<asp:TextBox ID="txtSuggestions" TextMode="multiline" runat="server" Height="105px" Width="301px"></asp:TextBox>
<br />
Would you recommend this service to your friend or family?
<br />
<asp:RadioButtonList ID="rblRecommend" runat="server">
<asp:ListItem Value="N">No</asp:ListItem>
<asp:ListItem Value="Y">Yes</asp:ListItem>
</asp:RadioButtonList>
<p></p>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<asp:Label ID="lblError" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click1" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
ASP.NET C#:
protected void btnSubmit_Click(object sender, EventArgs e)
{
...
try
{
connection.Open();
string cmdText = "INSERT INTO feedback (stars, best, worse, suggestions, recommend) VALUES (?stars, ?best, ?worse, ?suggestions, ?recommend);";
MySqlCommand cmd = new MySqlCommand(cmdText, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("?stars", MySqlDbType.VarChar).Value = **lblRating.Text**;
cmd.Parameters.Add("?best", MySqlDbType.VarChar).Value = txtBest.Text;
cmd.Parameters.Add("?worse", MySqlDbType.VarChar).Value = txtWorse.Text;
cmd.Parameters.Add("?suggestions", MySqlDbType.VarChar).Value = txtSuggestions.Text;
cmd.Parameters.Add("?recommend", MySqlDbType.VarChar).Value = rblRecommend.SelectedValue;
int result = cmd.ExecuteNonQuery();
lblError.Text = "Data Saved";
}
...
编辑: 这个JavaScript使用户能够选择的评级,输出会显示在lblRating
:
<script type ="text/javascript">
//we bind only to the rateit controls within the products div
$('#products .rateit').bind('rated reset', function (e) {
var ri = $(this);
//if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to null .
var value = ri.rateit('value');
var productID = ri.data('productid'); // if the product id was in some hidden field: ri.closest('li').find('input[name="productid"]').val()
//maybe we want to disable voting?
ri.rateit('readonly', true);
$.ajax({
url: 'rateit.aspx', //your server side script
data: { id: productID, value: value }, //our data
type: 'POST',
success: function (data) {
$('#response').append('<li>' + data + '</li>');
$('#lblRating').append(data);
},
error: function (jxhr, msg, err) {
$('#response').append('<li style="color:red">' + msg + '</li>');
}
});
});
</script>
为什么下面的ASP.NET C#代码不输出任何内容?
protected void Button2_Click1(object sender, EventArgs e)
{
Label1.Text = lblRating.Text;
}
谢谢。解释真的很有帮助。 – Bhav