简体   繁体   English

在点击函数中调用一个函数

[英]call a function inside a click function

I have a few images and a form where I have an input text, one pair of radio button and a select option, everytime I select one radio button and one option both of them get disabled and it can't be select anymore until the 'guardar cambios' button is clicked, when I select an option (in the sample it is the option 1) and an one image it changes for another but what I want is to, in case the person select the wrong option, to undo that, so I create a function where it has three parameters the value of radio and select and the class of the image selected, i call it inside 'seleccionar' where I reset the values of those three elements, then I put it inside the click function (the undo button).我有一些图像和一个表单,其中有一个输入文本、一对单选按钮和一个选择选项,每次我选择一个单选按钮和一个选项时,它们都会被禁用,直到 '点击guardar cambios的按钮,当我选择一个选项(在示例中它是选项1)和一个图像更改为另一个但我想要的是,如果这个人选择了错误的选项,撤销那个,所以我创建了一个函数,它有三个参数,radio 和 select 的值以及所选图像的类,我在“seleccionar”中调用它,在那里我重置这三个元素的值,然后我把它放在 click 函数中(撤消按钮)。 https://codepen.io/luzsdx/pen/ExKqwXP https://codepen.io/luzsdx/pen/ExKqwXP

I followed this How to call a function from click event inside another click event function?我遵循了如何从另一个单击事件函数中的单击事件调用函数?

But the problem is that when I choose an image it doesnt get replace for another, can anybody tell me what I'm doing wrong?但问题是,当我选择一个图像时,它不会被另一个图像替换,有人能告诉我我做错了什么吗? thank you and I get the 'classes is not defined' error in the console谢谢,我在控制台中收到“类未定义”错误

   function deshacer (classes, valorRadio, valorSelect) {
        $(valorRadio).prop("selectedIndex", 0).attr('disabled', false);
        $(valorSelect).val('none').attr('disabled', false)
        $(`div.${classes}>object`).attr('data', 'diente.svg')
    }


    $('.deshacer').on('click', function(e){
        e.preventDefault()
        deshacer(classes, valorRadio, valorSelect)
    })


    function seleccionar(valorSelect, valorRadio, classes, idImg) {
        if (valorSelect) {
            if (confirm('¿Seleccionar pieza Nro ' + classes + '?')) {
            } else {
                return false
            }
        }
        if (valorRadio == 'rojo') {
            if (valorSelect == 2) {
                alert('Extracción indicada es una prestación requerida.')
                $('#tratSelect').val('none')

            } else if (valorSelect == 1) {
                $(`div.${classes}>img`).attr('src', 'https://cdn-0.emojis.wiki/emoji-pics/lg/red-circle-lg.png')

            } 
        }
  deshacer(classes, valorRadio, valorSelect)

    } 

Global Variable全局变量

Initial global variables so calling values inside a function can be done, at top of your add those初始全局变量,因此可以在函数内调用值,在您添加这些值的顶部

var valorSelectValue;
var valorRadioValue;
var classesValue;
var idImgValue;

Now, mostly of time you can save, update selection of those variables sometimes they call it public/private variables depends in your needs and can be called it inside your function, in some other situations you might use this.valorSelectValue;现在,大多数时候你可以保存、更新这些变量的选择,有时他们称之为公共/私有变量取决于你的需要,并且可以在你的函数中调用它,在其他一些情况下你可能会使用this.valorSelectValue;

Local Variable: When you initial local variables that are defined within functions.局部变量:当您初始化在函数中定义的局部变量时。 They have local scope, meaning it should be used within the functions that define them.它们具有局部作用域,这意味着它应该在定义它们的函数中使用。

Global Variable: They are variables that are defined outside of functions.全局变量:它们是在函数之外定义的变量。 These variables have global scope, so they can be used by any function and no need to passing them as parameters of the function.这些变量具有全局作用域,因此任何函数都可以使用它们,无需将它们作为函数的参数传递。

Next, add new class named defaultImage接下来,添加名为defaultImage新类

.defaultImage {
content:url("https://www.tacoshy.de/Images/teeth.png");
    background-repeat: no-repeat;
    width: auto; /*or your image's width*/
    height: auto; /*or your image's height*/
    margin: 0;
    padding: 0;
}

Inside your function seleccionar I implemented new logic在你的函数seleccionar我实现了新的逻辑

function seleccionar(valorSelect, valorRadio, classes, idImg) {

...
...

} else if (valorSelect == 1) {

...

// Lookup for any active selection
if ($(`.active`)) {

// Remove added red-circle-lg.png from element
($(`.active`)).removeAttr("src")  

// Add defaultImage before removing active class
$(`.active`).addClass("defaultImage") 

// Remove active class
$(`.active`).removeClass("active") 
                
}
// Remove defaultImage class
$(`div.${classes}>img`).removeClass("defaultImage") 

// Add red-circle-lg.png Image
$(`div.${classes}>img`).attr('src', 'https://cdn-0.emojis.wiki/emoji-pics/lg/red-circle-lg.png')

// Add active class              
$(`div.${classes}>img`).addClass('active')
} 

Live Example: codepen.io现场示例: codepen.io

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

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