所以我有一个下拉列表和一个文本框:问题与jQuery的onchange()事件
<table>
<tr>
<td>Group Name: </td>
<td><%= Html.DropDownListFor(m => m.IndicationCalculatorGroupId, DropDownData.IndicationsGroup(SessionManager.Company.EntityID, ICConstants.IndicationsCalculatorGroupType), "", new { propertyName = "IndicationCalculatorGroupId", onchange = "UpdateField(this, false);GroupNameChange();" })%></td>
</tr>
<tr id="newGroupNameRow">
<td>New Group Name: </td>
<td><%= Html.TextBoxFor(m => m.IndicationCalculatorNewGroupName, new { @class = "economicTextBox", propertyName = "IndicationCalculatorNewGroupName", onchange = "UpdateField(this);" })%></td>
</tr>
</table>
我有JQuery的显示/在页面上隐藏基础上,滴在文本框向下选择。
function GroupNameChange()
{
$("#IndicationCalculatorGroupId").change(function() {
if ($("#IndicationCalculatorGroupId option:selected").text() == 'Create a New Group')
{
$("#newGroupNameRow").show();
}
else{
$("#IndicationCalculatorNewGroupName").val('');
$("#newGroupNameRow").hide();
}
});
}
但似乎你第一次改变下拉到“创建新组”,在文本框中不显示或做任何事情,当你选择一些其他的价值,然后只有选择“创建新组”代码开始工作。
什么不正确连接?
它的工作原理,不是当你从空的“空”选择,到“创建新的组”选择。 – slandau 2011-02-23 15:19:34
@slandau - 这是因为,正如我在我的回答中所解释的,您的更改处理程序不会在第一次调用GroupNameChange之后才设置。这就是为什么@ alexl的答案会起作用 - 它立即设置处理程序。 – justkt 2011-02-23 15:21:39