HTML:
<ul>
<li class="show-hide-container hide" style="display: list-item; ">
<ol>
<li class="password input required stringish" id="payment_gateway_login_input">
<input id="payment_gateway_login" name="payment_gateway[login]" type="password">
</li>
<li class="password input required stringish" id="payment_gateway_password_input">
<input id="payment_gateway_password" name="payment_gateway[password]" type="password">
</li>
</ol>
</li>
<li class="clearfix edit-login-pass"> <a href="#" class="btn" id="show_hide">Edit Login & Password</a>
</li>
</ul>
JQUERY:
$("input#payment_gateway_login").attr("id", "payment_gateway_login" + "_temp");
$("input#payment_gateway_password").attr("id", "payment_gateway_password" + "_temp");
$("#show_hide").live("click", function(event) {
return $("li.show-hide-container").toggle("show");
});
答案 0 :(得分:1)
你在找这样的东西吗?
$('#show_hide').on('click', function(event) {
if ($('#show-hide-container').is(':visible')) {
// container is currently visible so hide it and set id to temp
$('#show-hide-container').hide();
$('#payment_gateway_login').attr('id', 'payment_gateway_login_temp');
$('#payment_gateway_password').attr('id', 'payment_gateway_password_temp');
} else {
// container is currently hidden so show it and remove temp id
$('#show-hide-container').show();
$('#payment_gateway_login_temp').attr('id', 'payment_gateway_login');
$('#payment_gateway_password_temp').attr('id', 'payment_gateway_password');
}
});
有几点需要注意: .live()已弃用,因此您可能希望使用.on()。除非您将html中的原始ID更改为包含_temp。
,否则这也不起作用我不建议这种方法开始,但更改html ID很容易导致问题。一个更简单的解决方案是在您要保存信息时运行可见检查:
if ($('#show-hide-container').is(':visible')) {
// Save!
}
希望有所帮助!