简体   繁体   English

使用jQuery触发html onclick事件

[英]Using jQuery to trigger html onclick event

<input type="text" id="test_value" name="test_value" value="xyz" />
<input id="test_default" name="test_default" type="checkbox" onclick="with(this.form.elements['test_value']) { disabled = this.checked; if (this.checked) { value = ''; } else {if(value=='') {value=' '; value = '';}}};" />

The inline onclick is generated by a cms which I have no control of. 内联onclick是由我无法控制的cms生成的。 I want to execute $("#test_default").click(); 我想执行$("#test_default").click(); with jQuery but that doesn't work because it's not bound to the jQuery event manager. 使用jQuery,但这不起作用,因为它没有绑定到jQuery事件管理器。

I've been trying variations of 我一直在尝试变种

$("#test_default").click(function (e) {
                    $(e.target).attr("onclick").apply(checkbox, [e]);
                    return false;
                }).click();

but with no avail. 但无济于事。 Please Help. 请帮忙。

Thanks. 谢谢。

There are dozens of different functions that may be in inline in the onclick. onclick中可能有几十种不同的内联函数。 It isn't really an option to duplicate them and run them in parallel. 复制它们并并行运行它并不是一个真正的选择。

The answers work in a small demo but not in the live environment. 答案适用于小型演示,但不适用于实时环境。 Please look at http://jsfiddle.net/GtJjt/ I need the checkbox to be triggered programatically. 请查看http://jsfiddle.net/GtJjt/我需要以编程方式触发复选框。

Bizarrely only $("#metadata_field_text_10190_default")[0].click(); 奇怪的只有$("#metadata_field_text_10190_default")[0].click(); triggers the event correctly. 正确触发事件。 Can anyone explain this? 有谁能解释一下?

Call the native DOM's onclick directly ( see demo ): 直接调用原生DOM的onclick参见演示 ):

$("#test_default")[0].onclick();

Or, without jQuery at all, 或者,没有jQuery,

document.getElementById("test_default").onclick();

Update: 更新:

The actual problem is with the interaction between jQuery's .click() and the inline onclick code. 实际问题是jQuery的.click()和内联onclick代码之间的交互。 jQuery's .click() actually does two things: it first triggers any event handlers, including the inline onclick code, then it causes the checkbox to become checked. jQuery的.click()实际上做了两件事:它首先触发任何事件处理程序, 包括内联onclick代码, 然后它会导致复选框被选中。

The problem arises because the inline code tests whether the checkbox is checked. 出现问题的原因是内联代码测试是否选中了复选框。 Because this is triggered first, the code finds that the checkbox isn't checked , and doesn't do anything. 因为首先触发此操作,代码会发现未选中该复选框,并且不执行任何操作。 Now after this is finished, jQuery then goes and checks the checkbox. 完成后,jQuery然后去检查复选框。

How do we fix this? 我们如何解决这个问题? We check the checkbox manually first, then call the inline handler. 我们首先手动选中复选框,然后调用内联处理程序。 Unfortunately, we can't use jQuery's .click() to call the inline handler, because it will uncheck the checkbox again (it will also call other jQuery-bound event handlers if you add these in the future, you probably don't want that). 不幸的是,我们不能使用jQuery的.click()来调用内联处理程序,因为它会再次取消选中该复选框(如果你将来添加它们,它也会调用其他jQuery绑定的事件处理程序,你可能不想要那)。 Therefore we use the native DOM method I described earlier, since its only effect is to run the inline code. 因此,我们使用前面描述的本机DOM方法,因为它的唯一作用是运行内联代码。

Final code is this: 最终的代码是这样的:

$("#metadata_field_text_10190_default").attr("checked", true)[0].onclick();

See demo: http://jsfiddle.net/GtJjt/3/ . 请参阅演示: http//jsfiddle.net/GtJjt/3/ (I included a console.log in the inline code for the demo to prove that the inline code was running. Remove this if your browser is not Firefox or Chrome or it will crash). (我在演示的内联代码中包含了一个console.log ,以证明内联代码正在运行。如果您的浏览器不是Firefox或Chrome,它会删除它,否则会崩溃)。

Update again : 再次更新

I didn't see your solution earlier Simon, thanks for reminding me about [0].click() . 我早先没有看到你的解决方案西蒙,谢谢你提醒我[0].click() Essentially it is the native equivalent for jQuery's .click() and works because it does what I described above (checking the checkbox first, and then calling the inline code). 本质上它是jQuery的.click()的本机等价物,因为它完成了我上面描述的操作(首先检查复选框,然后调用内联代码)。 A warning though: this will also call any event handlers bound with jQuery, which may not be what you want. 但是有一个警告:这也会调用任何与jQuery绑定的事件处理程序,这可能不是你想要的。

我使用html中的触发器和onclick事件如下:

$("#test_default").trigger("onclick");

Just don't return false and it'll get called: 只是不要return false ,它会被调用:

$("#test_default").click(function (e) {
  //hi!
}).click();

If you want to prevent a default action, use e.peventDefault() instead, which won't kill the event. 如果要阻止默认操作,请改用e.peventDefault() ,这不会e.peventDefault()事件。 Also, with respect to your inline onclick handler (why not do it all unobtrusively?), I strongly recommend against using with . 此外,相对于你的网线onclick处理器(为什么不去做这一切悄悄地?),我强烈建议不要使用with

I use the next code: 我用下一个代码:

$("your_selector")[0].click();

In your case: 在你的情况下:

 $("#test_default")[0].click();

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

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