I'm trying to deal with one userscript user may have installed when he visits my site. As it works in a sandbox, I can't directly turn off some of the userscript's features that interfere with my site's native functionality. So the only option for me is to access them though the UI. First I needed to change one particular checkbox. First I tried to use jQuery's .val() method but it did not work. Then I did this:

if($('#someinput:checked').length) $('#someinput')[0].click();

And then it worked, the unwanted feature got turned off. Next one was <select>. Just .val() did'n help as expected, so I also triggered the change event right after I changed the value:

$('select[name=someselect]').val(0);
$('select[name=someselect]')[0].change();

But it still does not work. I guess I need to trigger change event somehow more properly. How do I do that?

有帮助吗?

解决方案 2

I found a solution here on SO:

Basically as far as I understand it fires all the native events properly and it's neatly wrapped into a jQuery plugin.

其他提示

You need to use:

$('select[name="someselect"]').change();

instead of:

$('select[name=someselect]')[0].change();

because $('select[name=someselect]')[0] return a DOM element, not a jQuery object.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top