I have a very strange issue with jQuery where I am triggering a click on a radio button but it is not firing completely and is not being captured by an on click function, however a similar call to jQuery trigger is being captured.

In the following jQuery I am selecting a <div> and using find to search for the suitable content.

var prev_chosen_d_option = $('#d_options_table .d_option_row[data-option-id="' + d_option_for_jq + '"]');

// this works, and the on click is captured
prev_chosen_d_option.find('.hover_target').trigger("click", true);

// this selects the radio button, but DOES NOT fire the on click function seen below    
prev_chosen_d_option.find('#d_standard_use_b_as_s_no').trigger("click", true);

These are my radio buttons:

<input type="radio" value="yes" id="d_standard_use_b_as_s_yes" name="d_standard_use_b_as_s">

<input type="radio" value="no" id="d_standard_use_b_as_s_no" name="d_standard_use_b_as_s">

$("#d_options_table .d_option_row .hover_target").on("click", function(e, isFirstLoad) {
   // it comes in here fine!
});

$('input[name=d_standard_use_b_as_s], input[name=d_next_day_use_b_as_s], #del_standard_use_b_as_s_no').on("click", function(e, isFirstLoad) {
 // it DOESN'T come in here
});

I can't see how jQuery is able to select the radio button and successfully check it, but the on method doesn't pick it up as a click...especially when I have a very similar setup running in close proximity in the code.

I know for sure that the radio buttons are within the selector as I can dump it out to the console with a console.log. Interestingly, when I dump out the events attached to it to the console I get undefined from this after the trigger:

console.log(prev_chosen_d_option.find("#_standard_use_b_as_s_no").data('events'));

(I am using jQuery 1.7.2 and testing in FF).

有帮助吗?

解决方案 2

The reason for this not working was simply I had the click handler below where I was actually triggering the click in the code.

其他提示

Instead of

    $('input[name=del_standard_use_b_as_s], input[name=del_next_day_use_b_as_s], #del_standard_use_b_as_s_no').on('click', function(e) {
//
    });

Try :

$(document).on('click', 'input[name=del_standard_use_b_as_s], input[name=del_next_day_use_b_as_s], #del_standard_use_b_as_s_no', function(e) {
 // 
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top