【问题标题】:Vue js: How to add a class to the closest td on click of ButtonVue js:如何在单击按钮时将类添加到最近的 td
【发布时间】:2023-03-11 00:45:01
【问题描述】:

我是来自 JS/JQuery 的 Vue 新手。我有一张桌子,每行有 2 个可能的按钮和两个输入,都包含在 <td> 中。当我单击一个按钮时,我希望最近的输入添加一个类。在 JQuery 中,我会使用 closest 方法来选择相邻的 <td> 这是我的 Vue 模板语法。非常感谢!

<tbody>
                     <tr v-for="child in registeredChildren">
                        <td class="col-2"><a :href="'/getchild/'+ child.child_id">{{child.childFirstName}}</a>&nbsp &nbsp {{child.childLastName}}</td>

                        <!--========TIME IN===========-->
                       <td class="col-3 pb-2"}"><input style="text-align:center;" class="form-control editChild initial" type="text" v-model="child.timeIn" ></td>
                       <td><button v-on:click="updateTimeIn(child)" class="btn btn-outline-success">Reset</button></td>

                        <!-- //========TIME Out ===========//-->
                       <td class="col-3 pb-2" ><input style="text-align:center;" class="form-control editChild" type="text" v-model="child.timeOut" ></td>
                       <td><button v-on:click="updateTimeOut(child)" class="btn btn-outline-success">Reset</button></td>
                    </tr>
 </tbody>
                

方法:我在想是否可以在 UpdateTimeIn 和 TimeOut 方法中添加一些代码,这可能是一种方法吗?

 methods:{
        updateTimeIn(child){
         this.updatedChild = child;
        console.log(child.child_id,child.timeIn)


            axios.post('http://kidsclub.test/upDateChildTimeIn', {
                child_id: child.child_id,
                timeIn: child.timeIn,
            })
                .then(function (response) {
                    console.log(response);
                })
                .catch(function (error) {
                    console.log(error);
                });
        },

**NB** I have the same for updateTimeOut

【问题讨论】:

  • 你能发布一个registeredChildren 的样例吗? child_id 是唯一的吗?
  • 另外,我认为这个答案描述了一个很好的方法。 stackoverflow.com/a/53617104/909973 您必须创建一个名为 childTableRow 的组件,然后将 child 对象作为道具传递给它。然后,您可以在 childTableRow 组件中使用局部变量来切换模板中的类。

标签: vue.js vuejs2


【解决方案1】:

您使用的是 Vue,与 jQuery 不同,这意味着数据应该驱动 DOM。在 Vue 中,您不必考虑选择某些 dom 节点。

我也曾经从 jQuery 切换到 Vue。我提供了一个demo,希望你能从中找到灵感。

<button @click="onClick">click me</button>
<div class="fixed-classes" :class="{'dynamic-class': isClick}"></div>
data() {
  return {
    isClick: false
  };
},
methods: {
  onClick() {
    this.isClick = !this.isClick;
  }
}

可以通过代码沙箱链接在线运行:codesandbox

我根据代码沙箱中的cmets更新了代码。

【讨论】:

  • 非常感谢您的回答,它非常有帮助。但是,我仍然有一个主要问题。单击时,我需要将动态类应用于与它的按钮相邻的输入。在点击的那一刻,将类应用于所有输入。我做了一些阅读,并尝试添加 `:key="child.child_id" 但这没有帮助。我提供了一张图片来帮助你理解。 link 非常感谢!!干杯!
  • 抱歉,我花了很长时间才看到 cmets。我看了看图,在代码沙箱里改了代码。请点击验证按钮,下面输入的背景颜色已经改变,甚至可以控制有效变量来控制不同的动态类显示不同的颜色,是否是你想要的效果。正如我在回答中所说,Vue 中的所有内容都通过数据驱动视图的变化。
  • 非常感谢您的努力,但遗憾的是我无法使其在沙箱中运行。我需要的是当一个按钮被舔时,只有相邻的输入会被设置样式。在我点击一个按钮的那一刻,所有的输入都被点击了。我想我会在 Vue 中放弃这个,而在 JS/JQuery 中简单地做。非常感谢!
  • 抱歉花了太多时间想出你想要的效果,我的英文不是很好,请见谅。我想我这次已经完全理解你的需求了。请再看一下沙箱中的代码,它可以点击reset来改变其相邻输入的样式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
相关资源
最近更新 更多