我想創建一個帶有一些文本框的簡單表單以及五星評級功能(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