jQuery 显示 div 然后消失

发布于 2024-10-10 12:15:10 字数 562 浏览 0 评论 0原文

如果这是一个简单的问题,我提前道歉,我有这个 javascript 代码:

$(document).ready(function() {
    $("#results").hide();

    var html = $.ajax({ url: "ajax.php?db_list=get", async: false}).responseText;

    $("#submit").click(function () { 
        $("#results").show(); 
    });
});

我有一个看起来像这样的按钮:

<fieldset class="action">
        <button name="submit" id="submit">Submit</button>
</fieldset>

当我单击“提交”按钮时,我想显示结果 div 并将其保留在那里,但在 Chrome 中它弹出然后立即消失,这是因为我的文档顶部的 hide() 函数准备好了吗?

谢谢!

I apologize ahead of time if this is a simple question, I have this javascript code:

$(document).ready(function() {
    $("#results").hide();

    var html = $.ajax({ url: "ajax.php?db_list=get", async: false}).responseText;

    $("#submit").click(function () { 
        $("#results").show(); 
    });
});

I have a button that looks this:

<fieldset class="action">
        <button name="submit" id="submit">Submit</button>
</fieldset>

When I click on the Submit button I wanted to show the results div and have it stay there, but in Chrome it pops up and then immediately disappears, is this because of the hide() function at the top of my document ready?

Thanks!

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

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

发布评论

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

评论(3

平安喜乐 2024-10-17 12:15:10

...这是因为我的文档顶部的 hide() 函数已准备好吗?

大概。我猜页面正在刷新。如果您不想这样做,请在处理程序中使用 return false;

$("#submit").click(function () { 
    $("#results").show();
    return false; 
});

event.preventDefault()

$("#submit").click(function ( event ) { 
    $("#results").show();
    event.preventDefault();
});

...is this because of the hide() function at the top of my document ready?

Probably. I'm guessing the page is refreshing. If you don't want that, use return false; in the handler.

$("#submit").click(function () { 
    $("#results").show();
    return false; 
});

or event.preventDefault().

$("#submit").click(function ( event ) { 
    $("#results").show();
    event.preventDefault();
});
兰花执着 2024-10-17 12:15:10

将您的结果最初设置为通过 CSS 隐藏

/* CSS */
#results {
  display: none;
}

不知道为什么您要跳到应该使用 $.ajax 的方式。如果是 GET 请求,请使用 $.get...与 .live 绑定,因为它更好:)。让它是异步的,因为你不希望你的服务器在出现错误时挂起......

$(document).ready(function() {
  $('#submit').live('click', function(){
    $.get('ajax.php?db_list=get', function(data){
      $('#results').html(data);
      $('#results').show();
      return false;
    });
  });
});

Have your results initially set to be hidden via CSS

/* CSS */
#results {
  display: none;
}

Not sure why you are jumping around the way you should use $.ajax. Use $.get if it's a GET request... Bind with .live because its better :). Let it be async because you don't want your server hanging in case of an error...

$(document).ready(function() {
  $('#submit').live('click', function(){
    $.get('ajax.php?db_list=get', function(data){
      $('#results').html(data);
      $('#results').show();
      return false;
    });
  });
});
顾北清歌寒 2024-10-17 12:15:10

使用此代码而不是提交

<input id='submitMe' type = 'button' value = 'Submit Me' />

现在您不必
返回假;
或者
event.preventDefault();在你的 JavaScript 中

Instead of submit, use this code

<input id='submitMe' type = 'button' value = 'Submit Me' />

Now you don't have to
return false;
or
event.preventDefault(); in your javascript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文