How do you wire up jQuery when selector changes

When I have an event handler set up through jQuery selecting DOM elements based on a data attribute (`$("input[data-action='enable']")`) The code below duplicates what I am doing. When the button is clicked, the appropriate `callback` function is invoked and the data-action and value elements of the buttons are updated. I was expecting to see that because the data-action value had changed that the click handler would toggle. Is there a way to set this up so that this does happen, or do I need to manually set up the handler after the click event? $("input[data-action='enable']").on("click", function() { var callback = function(context){ context.val("disable");"action", "disable"); }; ajaxCall(true, callback, $(this)); }); $("input[data-action='disable']").on("click", function() { var callback = function(context){ context.val("enable");"action", "enable"); }; ajaxCall(false, callback, $(this)); }); var ajaxCall = function(state, callback, context) { //would do an ajax call, and on success console.log("action")); callback(context); };
The main problem is that you're binding to the result set of a jQuery selector, which returns an array of jQuery objects at that specific time. You will probably need to unbind your events after your ajax call.

以上就是How do you wire up jQuery when selector changes的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准