简体   繁体   English

如何在Javascript中单击按钮后隐藏元素

[英]How to hide elements after button click in Javascript

I am unable to hide a list on button click on partial view.我无法在局部视图上单击按钮时隐藏列表。 This is what I am trying to do, but the following code does nothing.这就是我想要做的,但下面的代码什么也没做。 Can anyone please help me with this?任何人都可以帮我解决这个问题吗?

<script>       
     $(document).ready(function () {
        $('#resultBtn').onclick(function (e) {
            e.preventDefault();
            $('#List').hide();
            commit();
        })
      });
</script>

We might need a JSFiddle, the script seems to be good, but I guess the problem comes from your HTML5 code actually.我们可能需要一个 JSFiddle,脚本看起来不错,但我猜问题实际上来自您的 HTML5 代码。 Anyway, here are some advice that might help.无论如何,这里有一些建议可能会有所帮助。

#resultBtn is written in camelCase, so #List should be #list . #resultBtn是用驼峰大小写的,所以#List应该是#list Plus, if you have a problem, try to console.log('something') in the click callback function to see if something happen.另外,如果您遇到问题,请尝试在单击回调函数中执行console.log('something')以查看是否发生了某些事情。

Oh, I finally spotted the problem , you should easily find it if you open the console of your browser.哦,我终于发现问题了,打开浏览器的控制台应该很容易找到。 You can not do onclick() on a jQuery element, it's only click .您不能对 jQuery 元素执行onclick() ,它只能执行click A good habit could be to use edit on() (or bind() ) , you might find it useful someday.一个好习惯可能是使用edit on() (或bind() ,有一天你可能会发现它很有用。

$(function() {
    $('#resultBtn').on('click', function(e) {
        e.preventDefault();
        $('#list').hide();
        commit();
    });
});

Hope this helped!希望这有帮助!

Make the element invisible, but treat it as if it is still there:使元素不可见,但将其视为仍然存在:

document.getElementById("resultBtn").onclick = function() {
document.getElementById("List").style.visibility = "hidden";
return false;
}

Hide the element and treat it as if it is non-existent:隐藏元素并将其视为不存在:

document.getElementById("resultBtn").onclick = function() {
document.getElementById("List").style.display = "none";
return false;
}

You can set display property to none to hide the element您可以将 display 属性设置为 none 以隐藏元素

$(document).ready(function () {
            $('#resultBtn').onclick(function (e) {
                e.preventDefault();
                $('#List').css('display','none'); //<--- like here
                commit();
            })
          });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM