当用户将鼠标移过时,我正在通过Javascript更改ImageButtons的ImageUrl。在提交时,我想检查ImageUrl属性。但是,更改的属性不会反映在后面的代码中。事实上,我也是通过javascript更新span标签,其更改后的值也没有反映在帖子后面。这些都是静态html元素。我没有动态创建任何这些,也没有在后面的代码中对它们进行任何操作。
HTML :
<div style="margin: 20px 0 0 0; float: left;">
<div class="divStars1" style="width: 130px; float: left;">
<input type="image" name="ctl00$MainContent$ax1" id="MainContent_ax1" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
<input type="image" name="ctl00$MainContent$ax2" id="MainContent_ax2" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
<input type="image" name="ctl00$MainContent$ax3" id="MainContent_ax3" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
<input type="image" name="ctl00$MainContent$ax4" id="MainContent_ax4" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
<input type="image" name="ctl00$MainContent$ax5" id="MainContent_ax5" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
</div>
<div style="margin-top: 3px; width: 600px; float: left;">
<span>axs</span>
</div>
</div>
<div style="margin: 20px 0 0 0; float: left;">
<div class="divStars2" style="width: 130px; float: left;">
<input type="image" name="ctl00$MainContent$bx1" id="MainContent_bx1" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
<input type="image" name="ctl00$MainContent$bx2" id="MainContent_bx2" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
<input type="image" name="ctl00$MainContent$bx3" id="MainContent_bx3" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
<input type="image" name="ctl00$MainContent$bx4" id="MainContent_bx4" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
<input type="image" name="ctl00$MainContent$bx5" id="MainContent_bx5" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
</div>
<div style="margin-top: 3px; width: 600px; float: left;">
<span>bx blah blah</span>
</div>
</div>
使用Javascript:
$(document).on("mouseover", "input.stars", function () {
var score = 0;
$("input.stars").each(function (index, element) {
// element == this
if ($(element).attr("src") == "style/FilledStar.png") {
score = score + 1;
};
});
score = score / 5;
var n = score.toFixed(2);
$("#MainContent_lblScore").text(n);
});
btnSubmit_Click代码:
For Each ctrl As Control In Me.divSkill1.Controls
If (TypeOf ctrl Is ImageButton) Then
Dim imgCtl As ImageButton = TryCast(ctrl, ImageButton)
Dim x As String = imgCtl.ImageUrl 'HERE'S THE PROBLEM... x always equals original static value!
End If
Next
答案 0 :(得分:7)
服务器对此类控件的客户端更改一无所知。可能您必须在隐藏字段或隐藏文本框中存储已由javascript更改的值,然后在服务器端收集此值。
希望这有帮助
编辑:
如果你只想获得var n的值,你可以这样做:
// aspx;
<asp:HiddenField ID="HidScore" runat="server" />
/// javascript
$(document).on("mouseover", "input.stars", function () {
var hid=$("#<%= HidScore.ClientID %>");
var score = 0;
$("input.stars").each(function (index, element) {
// element == this
if ($(element).attr("src") == "style/FilledStar.png") {
score = score + 1;
};
});
score = score / 5;
var n = score.toFixed(2);
$("#MainContent_lblScore").text(n);
hid.val(n);
});
然后在vb.net中获取值:HidScore.value
答案 1 :(得分:5)
客户端对服务器端控件的更改通常不会保留或发回服务器。
考虑创建一个隐藏字段<asp:HiddenField />
控件,并让您的JavaScript也更新该值。 {@ 1}} / HiddenFields
将在PostBack上返回到服务器。然后,您的代码需要检查<input type="hidden" />
是否有可能的更改或更新,并将这些更改发送回HiddenField
ImageButton
属性。