简体   繁体   English

如何在jQuery中触发更改事件

[英]How to fire change event in jQuery

I have a UserControl placed in aspx page in asp.net. 我有一个UserControl放在asp.net的aspx页面中。 In this UserControl, I have a RadioButtonList and on change of RadioButtonList item, I want to execute the below jQuery function. 在此UserControl中,我有一个RadioButtonList,并且在更改RadioButtonList项目时,我想执行以下jQuery函数。

$('#rdlUser').change(function (e) {
    alert("change function");
    var radioBtnId = this.id;
    var $this = $(this);
    radconfirm('Are you sure you want to take leave?', function(arg){
        if (arg == true) {
            alert("User wants to take leave");
        }    
        else {
            alert("User doesn't want to take leave");
        }
    });
});

Simply you can do like this: 只需执行以下操作即可:

$(document).ready(function () {
    $('#rdlUser input').change(function () {
        alert($(this).val());
    });
});

For execute an event you can use trigger("eventname") function with JQuery. 为了执行事件,您可以在JQuery中使用trigger("eventname")函数。

Example

 $("#id").keypress(function(){ console.log("input keypress"); }); $("#btn").click(function(){ $("#id").trigger("keypress"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="id"> <button id="btn">Trigger keypress event</button> 

UPDATE UPDATE

With generated HTML you can't trigger event by using $("#generatedid") because element is not in the DOM at the first load. 使用生成的HTML,您无法通过使用$("#generatedid")来触发事件,因为第一次加载时元素不在DOM中。

You can use : 您可以使用 :

$(document).on("change",".your-radio-button-class",function(){
    //Make a test on the value of the select radio
    if($(this).val() == "connected")
    {

    }
    else
    {

    }
});

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

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