【问题标题】:Bootstrap modal does not take correct id dynamicallyBootstrap 模态不会动态获取正确的 id
【发布时间】:2017-06-25 16:59:29
【问题描述】:

我有这段代码是用 ruby​​ on rails 和 slim 编写的。在下面的代码中,我试图遍历我拥有的每一节课并显示它们。此外,每节课都有问题按钮,该按钮会生成一个弹出框(引导模式),该按钮接受问题输入、与该问题相关的课程 ID 并将其保存到数据库中。此代码的问题是每个问题按钮仅采用第一课的课程 ID。无论我点击哪一课的问题,它总是取第一课的课号。现在,我认为这里的问题是引导模式是 javascript,因此它会在加载整个内容后加载,这就是我收到此错误的原因。但我不确定如何解决这个问题。请帮我解决这个问题。谢谢。

`-lessons.each do |lesson|
  span.pull-right
    a[data-open="exampleModal2"]
      i.fa.fa-question[aria-hidden="true"]
  #exampleModal2.reveal[data-reveal]
        h1
        center
          | Ask question
        = simple_form_for Question.new, url: lesson_questions_path(lesson.id), method: :post do |f|
          = lesson.name
          = f.input :title, label: "Question"
          = f.input :content, label: "Question Description"
          = f.button :submit
          button.close-button aria-label=("Close modal") data-close="" type="button"
            span aria-hidden="true"  ×`

【问题讨论】:

    标签: javascript ruby-on-rails bootstrap-modal slim-lang


    【解决方案1】:

    您的问题是 id 属性在每次迭代后都不会更改,这会导致多个元素共享相同的 id,从而导致 Bootstrap 模式损坏和 HTML 无效。

    你应该改变这个:

    #exampleModal2.reveal[data-reveal]
    

    类似这样的:

    .reveal[data-reveal id=lesson.id]
    

    不要忘记相应地更新a[data-open="exampleModal2"]。我不记得 Bootstrap 是如何打开 modals 的,但如果是通过 data-open 属性,那么应该是它。

    【讨论】:

    • 这给了我一个错误提示 ActionView::Template::Error (Multiple id attributes specified): 并且页面没有显示任何内容。只是说出了点问题。
    • 那么这意味着你做了类似#exampleModal2.reveal[data-reveal id=lesson.id] 的事情,而不是我在回答中写的。阅读错误,阅读代码。都在里面,你只需要关注
    猜你喜欢
    • 2016-04-06
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 2016-04-30
    • 2018-06-02
    • 2016-10-01
    相关资源
    最近更新 更多