我在 asp.net 的 aspx 页面中放置了一个 UserControl。在这个 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");
}
});
});
最佳答案
要执行一个事件,您可以使用带有 JQuery 的 trigger("eventname")
函数。
示例
$("#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>
更新
对于生成的 HTML,您无法使用 $("#generatedid")
触发事件,因为元素在第一次加载时不在 DOM 中。
你可以使用:
$(document).on("change",".your-radio-button-class",function(){
//Make a test on the value of the select radio
if($(this).val() == "connected")
{
}
else
{
}
});
关于javascript - 如何在 jQuery 中触发更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39954139/