简体   繁体   English

如何使用jQuery从标记中调用onclick函数?

[英]How to call the onclick function from markup by using jQuery?

i fetched a form with a data in it from mysql and when i wanted to delete the form from the button used ajax to delete the data from the database and form. 我从mysql中获取了一个包含数据的表单,当我想从按钮中删除该表单时,使用了ajax从数据库和表单中删除了数据。 so i created onclick function. 所以我创建了onclick函数。 the query runs sucessfully but the onclick function is not being called. 查询成功运行,但未调用onclick函数。

this is my markup 这是我的标记

<div id="education">
    <div class="cvform edu-14">
        <input type="text" name="course-title-14" placeholder="Your course name"value="">
        <input type="text" name="course-inst-14" placeholder="Your Institution's name" value="">
        <input type="text" name="course-begin-14" placeholder="Course start year" value="">
        <input type="text" name="course-end-14" placeholder="Course ended. Blank for present" value="">
        <textarea name="course-detail-14" id="" cols="30" rows="10" placeholder="Add your course Details"></textarea>
        <a href="javascript:;"  onclick='removeEduPressed();'  class="links remedu" id="edu-14">Remove</a>
    </div>
</div>

this is the onClick function 这是onClick函数

function removeEduPressed() {
    $('.edu-14').remove();
}

and this is the ajax call 这是ajax电话

$(".remedu").click(function (event) {
    var str = event.target.id;
    var eduId = str.slice(4, str.length);//equals 14
    $.ajax({
        type: "POST",
        url: "posts/process.php",
        data: {
            eduId: eduId
        },
        success: (data) => {
        }
    });
});

You can use the jquery to remove the div with class name class="cvform edu-14" 您可以使用jquery删除类名称为class =“ cvform edu-14”的div

$("#edu-14").click(function () {
   $('.edu-14').remove();
});

use the above code instead of function calling on click. 使用上面的代码代替点击功能。 here is the link of working code. 这是工作代码的链接。 http://jsfiddle.net/gsLbuhed/ http://jsfiddle.net/gsLbuhed/

Just make sure that you have previously defined the function removeEduPressed, if the function is inside of a $(document).ready, when you are declaring the onclick the function going to be undefined, do you have this on your code? 只要确保您先前已定义函数removeEduPressed(如果该函数位于$(document).ready中),就可以了,当您声明onclick函数将要未定义时,您的代码上是否有此函数?

    $(document).ready(function () {
        function removeEduPressed() {
            $('.edu-14').remove();
        }
    });

Like that you are going to get this on the console inspector of your browser: ReferenceError: removeEduPressed is not defined[Learn More] 这样,您将在浏览器的控制台检查器上获取此信息: ReferenceError:未定义removeEduPressed [了解更多]

Just put declare the function before the form 只需在表单之前声明函数

function removeEduPressed() {
  $('.edu-14').remove();
}

Did you try using this ? 你试过用this吗?

$(".remedu").click(function(event){
    var str=$(this).attr('id');
    var eduId=str.split('-')[1];//equals 14
    $.ajax({
        type: "POST",
        url: "posts/process.php",
        data: {
            eduId: eduId
        },
        success: (data) => {
        }
    });
});

Also you could execute the remove after ajax execution getting a JSON reponse as example: 同样,您也可以在执行ajax之后执行删除,以获取JSON响应作为示例:

$(".remedu").click(function(event){
    var str=$(this).attr('id');
    var eduId=str.split('-')[1];//equals 14
    $.ajax({
        type: "POST",
        url: "posts/process.php",
        data: {
            eduId: eduId
        },
        success: (data) => {
            /*
                if(data.removeEdu)
                    $('.edu-14').remove();
            */
        }
    });
});

It's only an example, you can customize it. 这只是一个示例,您可以自定义它。

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

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