简体   繁体   English

如何一次处理多个@click事件<form>在 Vue.js 中使用@submit.prevent?</form>

[英]How do I handle multiple @click events in a single <form> with @submit.prevent in Vue.js?

I have a Vue.js project with a form in which I submit data using @click events into a database using Axios as well as display the change using AJAX.我有一个 Vue.js 项目,其中我使用@click事件将数据提交到使用 Axios 的数据库中,并使用 AJAX 显示更改。

This is the form:这是表格:

<div class="container-fluid px-0" v-for="(service, i) in servicesMeta">
    <div v-if="i == 0" :class="'pt-lg-4'"></div>
    <div class="container-lg bg-dark-white py-3 px-3 border-radius">
        <form action="admin.php" @submit.prevent="editData('services', service.Id, i, 'edit'), deleteData('services', service.Id, i, 'delete')">
            <div class="row no-gutters">
                <div class="col-md-9 col-12 d-flex align-items-center pb-md-0 pb-2">
                    <i class="fas fa-code-branch mr-3 ml-2 text-brand-primary" style="font-size: 1.9em"></i>
                    <h5 class="font-bolder text-truncate pr-md-0 mr-4">{{service.Name}}</h5>
                </div>
                <div class="col-md-3 col-12 d-flex align-items-center justify-content-end">
                    <button class="btn btn-brand-secondary btn-services font-bold py-1 mx-1" @click="editData('services', service.Id, i, 'edit')"><i class="fas fa-edit"></i></button>
                    <button class="btn btn-brand-primary btn-services font-bold py-1 mx-1" @click="deleteData('services', service.Id, i, 'delete')"><i class="fas fa-trash-alt"></i></button>
                </div>
                <div class="col-12"><hr></div>
                <div class="col-lg-4 col-sm-6 col-12 justify-content-lg-start justify-content-center pb-md-2 d-flex align-items-center pb-lg-3">
                    <div class="d-flex flex-column pt-3 px-3">
                        <h6 class="font-normal text-brand-primary pb-1">Popis :</h6>
                        <textarea type="text" class="border-radius" cols="18" rows="4" v-model="service.Description"></textarea>
                    </div>
                </div>
                <div class="col-lg-4 col-sm-6 col-12 d-flex flex-column justify-content-between align-items-lg-start align-items-center pb-md-2 pb-lg-3">
                    <div class="d-flex flex-column pt-3 px-3">
                        <h6 class="font-normal text-brand-primary pb-1">Místo :</h6>
                        <input type="text" class="border-radius" v-model="service.Place">
                    </div>
                    <div class="d-flex flex-column px-3">
                        <h6 class="font-normal text-brand-primary pb-1">Termíny :</h6>
                        <input type="text" class="border-radius" v-model="service.Terms">
                    </div>
                </div>
                <div class="col-lg-4 col-sm-6 col-12 d-flex flex-column justify-content-between align-items-lg-start align-items-center pb-md-2 pb-lg-3">
                    <div class="d-flex flex-column pb-lg-0 pt-lg-3 pt-sm-4 py-3 px-3">
                        <h6 class="font-normal text-brand-primary pb-1">Jméno :</h6>
                        <input type="text" class="border-radius" v-model="service.Name">
                    </div>
                    <div class="d-flex flex-column px-3 pb-md-0 pb-sm-4">
                        <h6 class="font-normal text-brand-primary pb-1">Cena :</h6>
                        <input type="text" class="border-radius" v-model="service.Price">
                    </div>
                </div>
                <div class="col-lg-4 col-sm-6 col-12 d-flex flex-column justify-content-lg-center justify-content-between align-items-lg-start align-items-center pb-md-2 pb-lg-3">
                    <div class="d-flex flex-column pt-lg-0 pt-sm-4 pt-3 pb-lg-2 px-3">
                        <h6 class="font-normal text-brand-primary pb-1">Sleva :</h6>
                        <input type="text" class="border-radius" v-model="service.Discount">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div v-if="i != servicesMeta.length - 1" :class="'mb-4'"></div>
    <div v-else :class="'mb-3'"></div>
</div>

I'm using a v-for loop since there is multiple items for which I want to create an individual container with the capability to add, edit and delete data for that specific item.我正在使用v-for循环,因为我想为多个项目创建一个能够为该特定项目添加、编辑和删除数据的单独容器。 The "trainersMeta" array of objects is generated by getting data from a MySQLi database with PHP on page load and then handing it to Vue.js using PHP's json_encode() method. “trainersMeta”对象数组是通过从 MySQLi 数据库中获取数据生成的,在页面加载时使用 PHP,然后使用 PHP 的json_encode()方法将其交给 Vue.js。 All of the data is fine and everything works.所有数据都很好,一切正常。

In the form I have 2 @click events with handlers:在表单中,我有 2 个带有处理程序的@click事件:

  1. editData编辑数据
  2. deleteData删除数据

I then handle the data using Vue.js within methods:{} :然后我在methods:{}

methods:{
    editData: function (requestSection, Id, i, handleType){
        if(requestSection == 'services'){
            console.log(handleType);
            axios.post('ajax.php',{
                request: 2,
                Id: Id,
                Name: this.servicesMeta[i].Name,
            })
            .then(function (response) {
                console.log(response);
            })
        }
    },
    deleteData: function (requestSection, Id, i, handleType){
        if(requestSection == 'services'){
            console.log(handleType);
            axios.post('ajax.php',{
                request: 5,
                Id: Id
            })
            this.servicesMeta.splice(i, 1);
        }
    }

The first parameter requestSection is just a string that I use to identify which section of the page is the data submitted from.第一个参数requestSection只是一个字符串,我用它来标识页面的哪个部分是提交的数据。 This is because I have another form with mostly the same structure on the page which I identify with a different string so that I can use the same methods without writing separate ones for each section.这是因为我在页面上有另一个表单,其结构几乎相同,我用不同的字符串标识,这样我就可以使用相同的方法,而无需为每个部分编写单独的方法。 Id is simply the id of the row retrieved from the database. Id只是从数据库中检索到的行的 id。 i is the loop index which I use to provide the AJAX like functionality. i是我用来提供类似 AJAX 功能的循环索引。 With it I remove the object from the array so that it disappears from the webpage after the delete button is clicked and the request to remove it from the database has been sent.有了它,我从阵列中删除了 object,以便在单击删除按钮并发送从数据库中删除它的请求后它从网页中消失。

The problem with the code is that for some odd reason anytime I click the button with @click=deleteData... the method editData is run instead?代码的问题是,出于某种奇怪的原因,每当我单击带有@click=deleteData...的按钮时,都会运行editData方法? I've verified and actually instead of request: 5 which relates to the deleteData method the request is set to request: 2 instead and if I change the data in the <input> fields on the page it is actually replaced inside of the database as well as on the page.我已经验证并且实际上不是request: 5这与deleteData方法有关,请求设置为request: 2而如果我更改页面上<input>字段中的数据,它实际上被替换为数据库内部以及在页面上。 Just like as if I ran the editData method originally.就像我最初运行editData方法一样。 I've added an additional parameter handleType which I used to identify which event is being run and logged the outpout to the browser's console and got 'edit' so edit was actually run instead of delete.我添加了一个额外的参数handleType ,我用它来识别正在运行的事件并将输出记录到浏览器的控制台并得到'edit' ,因此实际上运行的是编辑而不是删除。 Even worse the code was working just fine only like an hour ago and I was only fiddeling with some CSS in the meantime so I'm totally confused as to what is going on here.更糟糕的是,代码只在一个小时前工作得很好,同时我只是在摆弄一些 CSS,所以我对这里发生的事情完全感到困惑。

This is the PHP code which handles the AJAX database requests:这是处理 AJAX 数据库请求的 PHP 代码:

//Services edit data
if($request == 2){
    $id = $data->Id;
    $name = $data->Name;
    $stmt = $db -> prepare("REPLACE INTO services (id, name) VALUES(?, ?);");
    $stmt -> bind_param("is", $id, $name);
    $stmt -> execute();
    exit;
}

//Services delete data
if($request == 5){
    $id = $data->Id;
    $stmt = $db -> prepare("DELETE FROM services WHERE id = ?;");
    $stmt -> bind_param("i", $id);
    $stmt -> execute();
    exit;
}

But I believe that the error has nothing to do with the PHP code since invalid request is already being parsed to PHP from the JS.但我认为该错误与 PHP 代码无关,因为无效请求已从 JS 解析为 PHP。 At first I though I figured it out because I only had the editData method in the form's @submit.prevent but strangely even just with editData the code worked flawlessly with no errors/warnings before (I know weird).起初我虽然想通了,因为我只有表单的@submit.prevent submit.prevent 中的editData方法,但奇怪的是,即使只是使用editData ,代码也可以完美运行,之前没有错误/警告(我知道很奇怪)。 I've read on a website that you can put two different methods into the <form> @submit so I added the deleteData method to it also which did not make a difference.我在一个网站上读到,您可以将两种不同的方法放入<form> @submit ,因此我也向其中添加了deleteData方法,但这并没有什么区别。 What is going on here?这里发生了什么? What am I missing?我错过了什么?

I most likely ended up making a small typo probably missing an "s" in one of the section references.我很可能最终打了一个小错字,可能在其中一个部分参考中遗漏了一个“s”。 I've pulled a backup and redone all the changes and everything appears to work again.我已经提取了备份并重做了所有更改,一切似乎又可以正常工作了。 Would still be grateful if anybody could clearn up what is the @submit?prevent parameter used for then.如果有人能弄清楚当时使用的 @submit?prevent 参数是什么,我将不胜感激。 I genuinely could not find a proper answer online to it...我真的无法在网上找到正确的答案......

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

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