Jquery SlideToggle 在浏览器后退按钮后保留状态

发布于 2025-01-04 09:34:44 字数 2702 浏览 1 评论 0原文

我有一个滑动切换没有问题的 DIV。它向访问者展示了一些额外的选项。提交表单后,如果遇到错误,访问者可以按浏览器后退按钮进行纠正。带有所有附加选项的 div 并不处于最后留下的状态,而是处于折叠状态。我有什么想法可以防止这种情况发生吗?

    $("#sendChange").click(function () {
                          $("#sendSpec").slideToggle("slow");
                        if($('#sendWhen').html() == 'IMMEDIATLY'){
                           $('#sendWhen').html('AS SPECIFIED');
                        }else{
                           $('#sendWhen').html('IMMEDIATLY');
                        }
                        return false;
});




<div class="fcenter stdBox line25" style="margin-bottom:0px;" id="emlOptions">
<div>
     <label class="lab">Send email: </label><span id="sendWhen" class="blue">IMMEDIATLY</span> <a href="#" id="sendChange">[change]</a>
</div>
<div style="display:none" id="sendSpec">
    <label for="userTz" class="lab">Timezone: </label><select name="userTz" id="userTz" onchange="tzChange(this.value)" class="field" >
<option value="Pacific/Apia">(GMT-11:00) Midway Island, Samoa</option>
<option value="Pacific/Honolulu">(GMT-10:00) Hawaii</option>
...
</select>  <br />

<label for="delayTimeDate" class="lab">Time & Date: </label><input type="text" class="field" name="delayTimeDate" id="datepicker" />
<br /><br />
</div>

所以我尝试添加一个隐藏文本字段,该字段在 FF 中有效,但在 Opera 中无效。 Opera 会忘记隐藏字段值,但不会忘记文本字段值,因此我将文本字段包装在一个 display:none div 中,它可以记住该值,但在 Opera 浏览器更新表单字段的值之前会触发 javascript。这是代码:

function sendSpec () {
                          $("#sendSpec").slideToggle("slow");
                        if($('#sendWhen').html() == 'IMMEDIATLY'){
                           $('#sendWhen').html('AS SPECIFIED');
                           $('#sendWhenState').val('1');
                        }else{
                           $('#sendWhen').html('IMMEDIATLY');
                           $('#sendWhenState').val('0');
                        }}

$("#sendChange").click(function () {
                     sendSpec();
                    return false;
});
if($('#sendWhenState').val() == 1){
    sendSpec();
}

HTML 是相同的,但添加了表单字段,

<div style="display:none"><input type="text" id="sendWhenState" value="0" /></div>

我不喜欢这样,但找不到其他方法。上面的内容是相同的,只是在 if sendWhenState == 1 中添加了一个超时,以便 Opera 在我检查字段值之前更新字段,如下所示:

setTimeout(function(){
                    if($('#sendWhenState').val() == 1){
                       sendSpec();
                    }
         },1000);

我是新来的,我应该回答自己的问题还是我对用我想出的解决方案编辑问题?

I have a DIV that slideToggles no problem. It reveals some additional options to the visitor. Once the form is submit, if for example an error is encountered the visitor presses their browser back button to rectify. The div with all the additional options is not in the state is was last left, rather it is collapsed. Any ideas how I can prevent this?

    $("#sendChange").click(function () {
                          $("#sendSpec").slideToggle("slow");
                        if($('#sendWhen').html() == 'IMMEDIATLY'){
                           $('#sendWhen').html('AS SPECIFIED');
                        }else{
                           $('#sendWhen').html('IMMEDIATLY');
                        }
                        return false;
});




<div class="fcenter stdBox line25" style="margin-bottom:0px;" id="emlOptions">
<div>
     <label class="lab">Send email: </label><span id="sendWhen" class="blue">IMMEDIATLY</span> <a href="#" id="sendChange">[change]</a>
</div>
<div style="display:none" id="sendSpec">
    <label for="userTz" class="lab">Timezone: </label><select name="userTz" id="userTz" onchange="tzChange(this.value)" class="field" >
<option value="Pacific/Apia">(GMT-11:00) Midway Island, Samoa</option>
<option value="Pacific/Honolulu">(GMT-10:00) Hawaii</option>
...
</select>  <br />

<label for="delayTimeDate" class="lab">Time & Date: </label><input type="text" class="field" name="delayTimeDate" id="datepicker" />
<br /><br />
</div>

So I tried adding a hidden text field which worked in FF but not Opera. Opera forgets the hidden field values but not text field ones so I wrapped the text field in a display:none div which remembers the value ok but javascript fires before the formfield has its value updated by the Opera browser. Here is the code:

function sendSpec () {
                          $("#sendSpec").slideToggle("slow");
                        if($('#sendWhen').html() == 'IMMEDIATLY'){
                           $('#sendWhen').html('AS SPECIFIED');
                           $('#sendWhenState').val('1');
                        }else{
                           $('#sendWhen').html('IMMEDIATLY');
                           $('#sendWhenState').val('0');
                        }}

$("#sendChange").click(function () {
                     sendSpec();
                    return false;
});
if($('#sendWhenState').val() == 1){
    sendSpec();
}

HTML is the same but with the added formfield

<div style="display:none"><input type="text" id="sendWhenState" value="0" /></div>

I don't like this but couldn't find another way. the above is the same just added a timeout to the if sendWhenState == 1 so that Opera will have updated the fields before I check their value like so:

setTimeout(function(){
                    if($('#sendWhenState').val() == 1){
                       sendSpec();
                    }
         },1000);

I'm new here, should I have answered my own question or was I right to edit the question with the solution I came up with?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2025-01-11 09:34:44

我添加了一个隐藏文本字段,该字段在 FF 中有效,但在 Opera 中无效。 Opera 会忘记隐藏字段值,但不会忘记文本字段值,因此我将文本字段包装在一个 display:none div 中,它可以记住该值,但在 Opera 浏览器更新表单字段的值之前会触发 javascript。这是代码:

function sendSpec () {
                          $("#sendSpec").slideToggle("slow");
                        if($('#sendWhen').html() == 'IMMEDIATLY'){
                           $('#sendWhen').html('AS SPECIFIED');
                           $('#sendWhenState').val('1');
                        }else{
                           $('#sendWhen').html('IMMEDIATLY');
                           $('#sendWhenState').val('0');
                        }}

$("#sendChange").click(function () {
                     sendSpec();
                    return false;
});
if($('#sendWhenState').val() == 1){
    sendSpec();
}

HTML 是相同的,但添加了表单字段,

<div style="display:none"><input type="text" id="sendWhenState" value="0" /></div>

我不喜欢这样,但找不到其他方法。上面的内容是相同的,只是在 if sendWhenState == 1 中添加了一个超时,以便 Opera 在我检查字段值之前更新字段,如下所示:

setTimeout(function(){
                    if($('#sendWhenState').val() == 1){
                       sendSpec();
                    }
         },1000);

I added a hidden text field which worked in FF but not Opera. Opera forgets the hidden field values but not text field ones so I wrapped the text field in a display:none div which remembers the value ok but javascript fires before the formfield has its value updated by the Opera browser. Here is the code:

function sendSpec () {
                          $("#sendSpec").slideToggle("slow");
                        if($('#sendWhen').html() == 'IMMEDIATLY'){
                           $('#sendWhen').html('AS SPECIFIED');
                           $('#sendWhenState').val('1');
                        }else{
                           $('#sendWhen').html('IMMEDIATLY');
                           $('#sendWhenState').val('0');
                        }}

$("#sendChange").click(function () {
                     sendSpec();
                    return false;
});
if($('#sendWhenState').val() == 1){
    sendSpec();
}

HTML is the same but with the added formfield

<div style="display:none"><input type="text" id="sendWhenState" value="0" /></div>

I don't like this but couldn't find another way. the above is the same just added a timeout to the if sendWhenState == 1 so that Opera will have updated the fields before I check their value like so:

setTimeout(function(){
                    if($('#sendWhenState').val() == 1){
                       sendSpec();
                    }
         },1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文