我从下拉列表中获得输入,并根据用户的选择来显示/隐藏不同的输入。我已经尝试使用切换与布尔,但它没有响应,因为它应该。
我的期望,我发送真切换,然后,它要么显示输入,或如果它已经显示,它保持这种方式。如果我发出虚假,它会隐藏,或者如果它是隐藏的,它就会保持这种状态。
正如您所看到的,“父”和“子”的第一个值都为true,当我从一个子值切换到另一个父值时(当站点加载时,名字是可见的),它会隐藏,当我切换到管理员时,即使它是假的,它也会显示出来。
$(document).ready(function () {
var user = $("[id*=userTypeDD]").val();
$("[id*=userTypeDD]").on("change", function () {
user = $("[id*=userTypeDD]").val();
hideInputFields(user);
});
});
function hideUserInputFields(user) {
if (user == "Child") {
hideInputFields(true, true, false, false, false);
}
if (user == "Parent") {
hideInputFields(true, true, true, true, true);
}
if (user == "Administrator") {
hideInputFields(false, false, false, false, false);
}
}
function hideInputFields(firstNameIsVisible, surnameIsVisible, postcodeIsVisible, telephoneIsVisible, emailIsVisible)
{
$("[id*=firstNameTxt]").closest("tr").toggle(firstNameIsVisible);
}标记(ASP.NET)
<asp:DropDownList ID="userTypeDD" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Child</asp:ListItem>
<asp:ListItem>Parent</asp:ListItem>
<asp:ListItem>Administrator</asp:ListItem>
</asp:DropDownList>
</td>
<td> </td>
</tr>
<tr>
<td class="text-right" style="width: 574px">
<asp:Label ID="firstNameLabel" runat="server" Text="First name: "></asp:Label>
</td>
</tr>
</table>编辑:,在我看来,无论传递给的布尔值如何,每次都简单地切换TR。
发布于 2016-11-20 16:16:52
您在下拉列表的on change事件中调用了错误的函数。改变这个..。
$("[id*=userTypeDD]").on("change", function () {
user = $("[id*=userTypeDD]").val();
hideInputFields(user);
});至
$("[id*=userTypeDD]").on("change", function () {
user = $("[id*=userTypeDD]").val();
hideUserInputFields(user);
});这就是为什么最好不要将您的函数命名为类似的。您的hideUserInputFields是一个误导性的名称,因为它不是这样做的函数。你最好叫它handleUserSelectionChange。那你就不会犯这个错误了。您的另一个函数名是有意义的。
https://stackoverflow.com/questions/40706213
复制相似问题