GVKun编程网logo

避免在 html 按钮消失之前再次单击它django(html按钮禁止点击)

1

在本文中,您将会了解到关于避免在html按钮消失之前再次单击它django的新资讯,同时我们还将为您解释html按钮禁止点击的相关在本文中,我们将带你探索避免在html按钮消失之前再次单击它djang

在本文中,您将会了解到关于避免在 html 按钮消失之前再次单击它django的新资讯,同时我们还将为您解释html按钮禁止点击的相关在本文中,我们将带你探索避免在 html 按钮消失之前再次单击它django的奥秘,分析html按钮禁止点击的特点,并给出一些关于Angular 8 - 避免在 ngFor 中对数组进行排序时重新渲染特定项目、Cats 效果:避免在 T => Unit 的外部侦听器上调用 unsafeRun、css3 – 避免在:: before伪元素和下一个单词之间进行换行、Flutter:避免在 DraggableScrollableSheet的实用技巧。

本文目录一览:

避免在 html 按钮消失之前再次单击它django(html按钮禁止点击)

避免在 html 按钮消失之前再次单击它django(html按钮禁止点击)

如何解决避免在 html 按钮消失之前再次单击它django

我正在一个 Django 项目中工作,该项目具有可由用户加入的事件(将 user_id 和 event_id 添加到模型参加),在我的活动中,我有一个加入按钮表单,点击后添加参加按钮消失并出现一个名为离开事件和相反的新按钮。当用户加入或离开活动页面时,所有这些都有效。

我遇到的问题是,我可以在按钮消失之前连续单击按钮 2 次,因为它正在尝试添加一个已经存在的参与对象或删除一个不存在的对象存在。

所以我的问题是,是否有办法避免用户点击按钮两次,这样它就不会发送 POST 请求 2 次,或者至少如果他在它消失之前再次单击它而不发送请求,或者有可能是一种在发送 POST 请求之前应用按钮 onclick 功能的方法?

编辑:我解决了 Django 部分,所以如果用户单击加入或离开按钮 2 次,我不会收到错误,但现在用户仍然可以继续单击该按钮并且页面将继续重新加载直到他/她停止点击我认为不好的同一个按钮。

这里的 javascript 显示了在页面加载或单击任一按钮时执行的加入或离开按钮(如果登录的用户正在参加,我首先查看了服务员列表,因为我没有找到方法在 Django 模板中查询出席模型):

script type="text/javascript">
    window.onload = function() {
        showJoinLeave();
    };
    function showJoinLeave() {
        var attendants = document.getElementsByClassName("attendant-username");
        var username = "{{ user.username }}";
        var joinButton = document.getElementById("event-info-join");
        var leaveButton = document.getElementById("event-info-leave");

        var is_attending = false;
        var attendant;
        for (attendant of attendants) {
            var attendant_name = attendant.innerHTML;
            if (attendant_name == username) {
                is_attending = true;
            }
        }
        
        if (is_attending===true){
            joinButton.style.display = "none";
            leaveButton.style.display = "flex";
        }
        
        else {
            joinButton.style.display = "flex";
            leaveButton.style.display = "none";
        }
    }

我的两个按钮:

<form action="{% url ''event-join'' object.id %}" method="POST">
    {% csrf_token %}
    <button type="submit" id="event-info-join"title="Join Event" onclick="showJoinLeave()">
        <ititle="Join Event">add</i>Join Event
    </button>
</form>
<form action="{% url ''event-leave'' object.id %}" method="POST">
    {% csrf_token %}
    <button type="submit" id="event-info-leave"title="Leave Event" onclick="showJoinLeave()">
        <ititle="Leave Event">add</i>Leave Event
    </button>
</form>

解决方法

我不知道具体的 django,但通常你会在前端处理这个问题,只需将按钮设置为在点击后立即禁用即可。

通过这种方式,您不仅涵盖了技术方面,还涵盖了 UX 和可访问性方面。通过将按钮设置为禁用,用户可以立即获得对其点击的反馈,并且知道他不能再次点击它。这也适用于残障用户(例如使用屏幕阅读器等)

我不建议设置 pointer-events: none 或执行 onclick = null,因为它没有涵盖上述方面。

joinButton.disabled = true;
,

在隐藏按钮之前,我会选择 pointer-events: none !important;,如下所示:

window.onload = function() {
    showJoinLeave();
};
function showJoinLeave() {
    var attendants = document.getElementsByClassName("attendant-username");
    var username = "{{ user.username }}";
    var joinButton = document.getElementById("event-info-join");
    var leaveButton = document.getElementById("event-info-leave");

    var is_attending = false;
    var attendant;
    for (attendant of attendants) {
        var attendant_name = attendant.innerHTML;
        if (attendant_name == username) {
            is_attending = true;
        }
    }
    
    if (is_attending===true){
        joinButton.style.pointerEvents = "none";
        joinButton.style.display = "none";
        leaveButton.style.pointerEvents = "auto";
        leaveButton.style.display = "flex";
    }
    
    else {
        joinButton.style.pointerEvents = "auto";
        joinButton.style.display = "flex";
        leaveButton.style.pointerEvents = "none";
        leaveButton.style.display = "none";
    }
}

指针事件:无

阻止指定 HTML 上的所有点击、状态和光标选项 元素

要了解有关 pointer-events 的更多信息,请查看以下链接:

https://css-tricks.com/almanac/properties/p/pointer-events/

Angular 8 - 避免在 ngFor 中对数组进行排序时重新渲染特定项目

Angular 8 - 避免在 ngFor 中对数组进行排序时重新渲染特定项目

如何解决Angular 8 - 避免在 ngFor 中对数组进行排序时重新渲染特定项目

这是我的 HTML 代码:

  1. *ngFor="let project of filteredProjects; trackBy: trackByProjectID"

这是打字稿代码:

  1. trackByProjectID(index: number,project: Project): string {
  2. return project.project_id;
  3. }

当我向下滚动然后对整个数组进行排序时,我正在使用 ionic 无限滚动来加载更多项目。

  1. sort(sortBy,sortOrder) {
  2. this.sortBy = sortBy; // descending
  3. this.sortOrder = sortOrder; // key name to sort
  4. const filteredProjectscopy = JSON.parse(JSON.stringify(this.filteredProjects));
  5. this.filteredProjects = [];
  6. this.filteredProjects = _.orderBy(filteredProjectscopy,[sortBy],[sortOrder]);
  7. }

在我渲染的 DOM 中,我通过选择一个选项在列表项上打开一个子菜单,但是一旦我向下滚动,我的已过滤项目就会更新(由于滚动后加载更多项目),然后数组得到由于排序方法,也重新排序。因此,我的子菜单会在该特定列表项上关闭。

但是,如果我不对数组进行排序,子菜单不会关闭。但是,在这种情况下需要排序。

即使在加载更多项目并对整个数组进行排序后,我也希望子菜单保持打开状态。

请提供解决方案。

Cats 效果:避免在 T => Unit 的外部侦听器上调用 unsafeRun

Cats 效果:避免在 T => Unit 的外部侦听器上调用 unsafeRun

IO 不仅是您的听众的容器。来自 cats-effect 文档:

A pure abstraction representing the intention to perform a side effect,where the result of that side effect may be obtained synchronously (via return) 
or asynchronously (via callback)

这意味着 IO 不包含某些值,而是包含执行某些副作用的意图。

此处是使用 IO 的最常用方法:

  1. 使用 IO(这种方式命名为 composition)向包含 flatMap 的内容添加一些效果
val ioA: IO[A] = ??? // you have some side effect returning A
// here you can have some different side effect returning B: work with DB,read some resource or so on
def aToIOB(a: A): IO[B] = ??? 
val ioB: IO[B] = ioA.flatMap(aToIOB)
  1. 启动整个组合链副作用1.使用unsafeRunSync或运行{{1}的类似函数}}。它应该在您的应用程序中调用一次,并且通常在宇宙的尽头(在您的程序的末尾)。
IO

因此,当您看到 // this line launches execution of ioA and aToIOB and will return value of B with side-effects val b: B = ioB.unsafeRunSync() 时,您应该记住它与 IO[A]Option[A] 不同,您只需执行 List[A] 即可获得 {{1} }.当您的应用程序未执行结束时,您应该使用 IO 的组合。如果是这样,您可以使用 A 运行它。

我的意思是你应该在不超过一次使用 IO 的情况下构建你的应用程序,我猜,你的签名

unsafeRunSync

不应该使用unsafeRunSync API,但非常类似于def listen(listener: ResponseType => Unit): Handle API,看看函数IO

Async[F[_]]

尽量将 async 应该在您的应用程序中做的事情集中起来,并选择正确的签名。

有用的链接:

  • cats-effect IO monad
  • cats-effect Async typeclass

css3 – 避免在:: before伪元素和下一个单词之间进行换行

css3 – 避免在:: before伪元素和下一个单词之间进行换行

问题

我使用:: before来生成一个伪元素,以显示链接锚文本左侧的小图像.但是,当重新调整浏览器窗口的宽度时,该行可能会被包裹在小图像和链接锚文本的第一个单词之间,这是不希望的.

期望的结果

我想将小图像和链接锚文本的第一个单词保持在同一行.

我保持显示:内联在a-tag上,因为链接锚文本应该能够包裹行,而不是整个原子盒,但是它的第一个单词填满当前行,任何剩余的单词继续在下一行.

此外,链接后的文本应与链接锚文本的最后一个单词在同一行继续(前提是当前第一个单词有空格).

注1)如果我在a-tag上使用display:inline-block,整个链接锚文本将作为一个整体包装,这是我不想要的.

注2)我在伪元素上使用display:inline-block,因为我需要设置它的宽度和高度.

注3)我试过设置white-space:pre在伪元素上,但是没有解决问题.

谢谢!

代码

代码位于JSFiddle,并在下面重复以确保完整性.

我的HTML:

<p>
Text text <ahref="#">FirstWord word2 word3</a> text after link.
</p>

我的CSS:

.pre-icon::before {
    content: "";
    display: inline-block;
    width: 6px;
    height: 10px;
    background-color: transparent;
    background-image: url(data:image/gif;base64,R0lGODlhBgAKAJEAADM2Zv///////wAAACH5BAUUAAIALAAAAAAGAAoAAAINBIRimdvHFetoUWglKwA7);
    background-repeat: no-repeat;
    margin-left: .13em;
    margin-right: .25em;
    white-space: Nowrap;
}

解决方法

问题是在伪元素和内容之间发生换行,所以你不能通过仅将伪空间设置为伪元素来避免它.

相反,您可以将锚的内容包装在span中,将white-space:Nowrap设置为整个锚,并恢复初始空白:span中的normal.

.pre-icon {
  white-space: Nowrap;
}
.pre-icon > span {
  white-space: normal;
}
.pre-icon::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 10px;
  background-color: transparent;
  background-image: url(data:image/gif;base64,R0lGODlhBgAKAJEAADM2Zv///////wAAACH5BAUUAAIALAAAAAAGAAoAAAINBIRimdvHFetoUWglKwA7);
  background-repeat: no-repeat;
  margin-left: .13em;
  margin-right: .25em;
}
.pre-icon {
  white-space: Nowrap;
}
.pre-icon > span {
  white-space: normal;
}
Text text <ahref="#"><span>FirstWord word2 word3</span></a> text after link.

Flutter:避免在 DraggableScrollableSheet

Flutter:避免在 DraggableScrollableSheet

如何解决Flutter:避免在 DraggableScrollableSheet

我正在使用永久停留在底部屏幕上的 DraggableScrollableSheet。我检索了工作表内容的实际高度,所以我可以设置maxChildSize,否则即使下面没有内容,面板也会一直向上滑动。

它有点工作,但看起来不太好。你可以看到我仍然可以向上滚动,灰色背景显示在内容下方。我想避免这种情况:

enter image description here

基本上,当我达到最大子尺寸时,我想禁用内容滚动。这能实现吗?

我的代码:

import ''package:Flutter/material.dart'';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: ''Flutter Demo'',theme: ThemeData(
        primarySwatch: Colors.blue,),home: MyHomePage(),);
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(''Bottomsheets''),body: Stack(
        children: <Widget>[
          Flex(direction: Axis.horizontal,children: [
            Expanded(
                child: DecoratedBox(
                    child: Container(),decoration: Boxdecoration(color: Colors.black45))),]),HomePageDraggableBottomSheet(),],);
  }
}

class HomePageDraggableBottomSheet extends StatefulWidget {
  @override
  _HomePageDraggableBottomSheet createState() =>
      _HomePageDraggableBottomSheet();
}

class _HomePageDraggableBottomSheet
    extends State<HomePageDraggableBottomSheet> {
  GlobalKey _contentKey = GlobalKey();
  Size? _size;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((_) {
      final size = _contentKey.currentContext?.size;
      print(''size is $size'');
      setState(() {
        _size = size;
      });
    });
  }

  double _calculateMinChildSize() {
    final screenHeight = MediaQuery.of(context).size.height;
    final containerHeight = _size != null ? _size!.height : null;
    final ratio = containerHeight == null ? 0.15 : containerHeight / screenHeight;
    print(''screen height: $screenHeight,bottom height: $containerHeight,ratio is $ratio'');
    return ratio;
  }

  @override
  Widget build(BuildContext context) {
    final minSize = _calculateMinChildSize();

    return DraggableScrollableSheet(
        initialChildSize: minSize,minChildSize: minSize * 0.35,maxChildSize: minSize,builder: (context,controller) {
          return SingleChildScrollView(
            controller: controller,child: HomePageBottomSheetContent(contentKey: _contentKey),);
        });
  }
}

class HomePageBottomSheetContent extends StatelessWidget {
  HomePageBottomSheetContent({this.contentKey});

  final GlobalKey? contentKey;

  @override
  build(BuildContext context) {
    return Container(
        key: contentKey,decoration: Boxdecoration(color: Colors.white),child: Column(
            mainAxisAlignment: MainAxisAlignment.end,mainAxisSize: MainAxisSize.max,children: [
              Flex(direction: Axis.horizontal,children: [
                Expanded(
                    child: Container(
                        child: Center(child: Icon(Icons.drag_handle))))
              ]),Container(
                  width: double.infinity,padding: const EdgeInsets.symmetric(
                      vertical: 10.0,horizontal: 5.0),child: OutlinedButton(
                      child: Text(''Submit''),onpressed: () {
                        print(''Submitted'');
                      })),]));
  }
}


关于避免在 html 按钮消失之前再次单击它djangohtml按钮禁止点击的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Angular 8 - 避免在 ngFor 中对数组进行排序时重新渲染特定项目、Cats 效果:避免在 T => Unit 的外部侦听器上调用 unsafeRun、css3 – 避免在:: before伪元素和下一个单词之间进行换行、Flutter:避免在 DraggableScrollableSheet的相关知识,请在本站寻找。

本文标签:

上一篇使用 Django Jinja 或 Python 在 Html 下拉列表选项中设置选定值(django选择下拉框的值)

下一篇