如何在没有函数的情况下在jquery中绑定事件?

时间:2022-01-08 08:58:43

I am trying to find the way of binding event to an element. Can we bind a JQuery without mentioning function?

我试图找到将事件绑定到元素的方法。我们可以在不提及函数的情况下绑定JQuery吗?

Here is my fiddle

这是我的小提琴

I need to make Loan binding enable on click so that it can calls same radio button.

我需要在点击时启用贷款绑定启用,以便它可以调用相同的单选按钮。

It's getting unbind successfully but could not be rebind to call the same click method in which it is defined.

它已成功取消绑定但无法重新绑定以调用定义它的相同单击方法。

My code :

我的代码:

$('input[type="radio"]').click(function(){
  var radioButId = $(this).attr('id');
  var value = $(this).val();
    alert(value + " is clicked");

    if(value === "home")
    {
        alert("Enable Loan Link")
        $( "#ba").bind( "click" );
    }
    if(value === "dog")
    {
        alert("Disable Loan Link");
        $( "#ba").unbind( "click" );
    }   
});

Could someone please help.

请有人帮忙。

3 个解决方案

#1


A possible way

一种可能的方式

Fiddle : https://jsfiddle.net/uhxpmexe/7/

小提琴:https://jsfiddle.net/uhxpmexe/7/

JS :

function fHardCodedFunction(){
alert("bindee");
}

function myfunction () {
  var radioButId = $(this).attr('id');
  var value = $(this).val();
    alert(value + " is clicked");
    $( "#ba").unbind( "click" );
    if(value === "home")
    {
        alert("Enable Loan Link")
        $( "#ba").on( "click", myfunction );
    }
    if(value === "dog")
    {
        alert("Disable Loan Link");
    }  
} 

$('input[type="radio"]').click(myfunction);

#2


You can simply enable and disable "Loan" with the disabled attribute.

您可以使用disabled属性简单地启用和禁用“Loan”。

I have updated your fiddle

我已经更新了你的小提琴

function fHardCodedFunction(){
alert("bindee");
}

$('input[type="radio"]').click(function(){
  var radioButId = $(this).attr('id');
  var value = $(this).val();
    console.log(value + " is clicked");
    
    if(value === "home")
    {
        console.log("Enable Loan Link")
        $("#ba").removeAttr("disabled");
    }
    if(value === "dog")
    {
        console.log("Disable Loan Link");
        $( "#ba").attr( "disabled","disabled" );
    }   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
When Dog clicked, Load should disable and when home clicked, Load should be enable <br>
<input type="radio" class="p27" id ="ba" name="ins_type1" value="loan" checked="checked" /> Loan<br><br>

Click here:
<input type="radio" class="p27" id ="ba2" name="ins_type2" value="home" /> Home 
<input type="radio" class="p27" id ="ba3" name="ins_type2" value="dog" />
Dog

Notice that when the disabled attribute is present, clicking the button does not do anything. Besides, it visually greys out the button.

请注意,当存在disabled属性时,单击该按钮不会执行任何操作。此外,它在视觉上使按钮变灰。

I have also used console.log, because debugging with alert sucks :)

我也使用了console.log,因为调试警报很糟糕:)

#3


use on and off instead of bind and unbind

使用on和off而不是bind和unbind

check the following link

检查以下链接

https://api.jquery.com/off/

#1


A possible way

一种可能的方式

Fiddle : https://jsfiddle.net/uhxpmexe/7/

小提琴:https://jsfiddle.net/uhxpmexe/7/

JS :

function fHardCodedFunction(){
alert("bindee");
}

function myfunction () {
  var radioButId = $(this).attr('id');
  var value = $(this).val();
    alert(value + " is clicked");
    $( "#ba").unbind( "click" );
    if(value === "home")
    {
        alert("Enable Loan Link")
        $( "#ba").on( "click", myfunction );
    }
    if(value === "dog")
    {
        alert("Disable Loan Link");
    }  
} 

$('input[type="radio"]').click(myfunction);

#2


You can simply enable and disable "Loan" with the disabled attribute.

您可以使用disabled属性简单地启用和禁用“Loan”。

I have updated your fiddle

我已经更新了你的小提琴

function fHardCodedFunction(){
alert("bindee");
}

$('input[type="radio"]').click(function(){
  var radioButId = $(this).attr('id');
  var value = $(this).val();
    console.log(value + " is clicked");
    
    if(value === "home")
    {
        console.log("Enable Loan Link")
        $("#ba").removeAttr("disabled");
    }
    if(value === "dog")
    {
        console.log("Disable Loan Link");
        $( "#ba").attr( "disabled","disabled" );
    }   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
When Dog clicked, Load should disable and when home clicked, Load should be enable <br>
<input type="radio" class="p27" id ="ba" name="ins_type1" value="loan" checked="checked" /> Loan<br><br>

Click here:
<input type="radio" class="p27" id ="ba2" name="ins_type2" value="home" /> Home 
<input type="radio" class="p27" id ="ba3" name="ins_type2" value="dog" />
Dog

Notice that when the disabled attribute is present, clicking the button does not do anything. Besides, it visually greys out the button.

请注意,当存在disabled属性时,单击该按钮不会执行任何操作。此外,它在视觉上使按钮变灰。

I have also used console.log, because debugging with alert sucks :)

我也使用了console.log,因为调试警报很糟糕:)

#3


use on and off instead of bind and unbind

使用on和off而不是bind和unbind

check the following link

检查以下链接

https://api.jquery.com/off/