gpt4 book ai didi

javascript - 在 Rating 控件中单击 Star 时如何传递值?

转载 作者:行者123 更新时间:2023-11-27 23:59:45 25 4
gpt4 key购买 nike

当我将星级评定为 5 时,id 值应作为 10 传递。它将值作为 1 传递。如果我将星级评定为 4,相同的值 (1) 将传递给 Controller ​​。这是对所有额定值的重复,
我删除了所有以前的代码并添加了更新后的代码。请检查一下这是我的查看代码:

<form method="post" id="ratethecommand" action="@Url.Action("rated", "Rating")">
<table width="100%" cellspacing="10"> <tr>
<td valign="top" width="">
<table width="100%">
<tr>
<td width="50%">

<a href="@Url.Action("rated/1", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
<a href="@Url.Action("rated/2", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
<a href="@Url.Action("rated/3", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3"/></a>
<a href="@Url.Action("rated/4", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
<a href="@Url.Action("rated/5", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
<a href="@Url.Action("rated/6", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
<a href="@Url.Action("rated/7", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
<a href="@Url.Action("rated/8", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
<a href="@Url.Action("rated/9", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" /></a>
<a href="@Url.Action("rated/10", "Rating")"><input class="star {split:2}" type="radio" name="test-4-rating-3" value="5.0"/></a>

<input type="text" name="rating"/>
</td>

<td valign="top" width="5">&nbsp;</td> <td valign="top" width="160">
<u>Test results</u>:<br/><br/>
<div class="test Smaller">

<span style="color:#FF0000">Results will be displayed here</span>
</div>
</td>
</tr>
</table>

这是我的 Controller 代码:

public ActionResult rated(int id)
{
double ratedvalue = Convert.ToDouble(id*0.5);
Tbl_Rating tb = new Tbl_Rating();
var value = new Tbl_Rating
{
Rating = ratedvalue,
TaskId = 1,
UserId = 1,
DReportID = 1,
CreatedBy = 1,
CreatedOn = DateTime.Now,
ModifiedBy = 1,
ModifiedOn = DateTime.Now
};

db.Tbl_Rating.Add(value);
db.SaveChanges();

return View();
}

最佳答案

当问题不同时,这是第一个解决方案。

使用 jQuery 很简单:当用户点击您的电台/明星时,您提交表单。

$(".star").click(function() {
//your logic on star click
$("#ratethecommand").submit();
});

你也可以改变你的实际 filosofy,做一个简单的重定向而不是 sumbitting 形式:在这种情况下,你应该将你的实际 radio 输入(星星)转换为指向你的 Controller / Action 并将值作为参数的元素(如果是整数更好):

<a href="@Url.Action("rated/3", "Rating")">icon of your star</a>

如果您有默认路由,那么您可以在您的操作中从 id 检索费率(顺便说一下:将它大写!)。

最后一个想法:你也可以选择异步方式,同样用jQuery post和get方法实现起来会很容易!

最终解决方案(最后一个问题的答案编辑)

从 A 元素内容中移除 INPUT 元素。 Insted 放置文字/星星图标。

希望对您有所帮助,

阿尔贝托

关于javascript - 在 Rating 控件中单击 Star 时如何传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21930256/

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