简体   繁体   English

第一次尝试时,链接点击事件未触发按钮点击事件(仅限IE)。 第二次单击即可工作。

[英]link click event not firing button click event on the first try (IE only). Works on second click.

This is driving me mad. 这让我发疯。 I've got a div on my page that opens up in colorbox. 我的页面上有一个div,它在colorbox中打开。 When the user clicks one of the links in the div, it fires an event to the code (below) which should in turn populate the hidden field and then click a server button to run some code behind. 当用户单击div中的链接之一时,它将向代码触发事件(如下),该事件应依次填充隐藏字段,然后单击服务器按钮以在后面运行一些代码。

Problem is, in IE(9) it won't click the server button on the first attempt (yes, it does go onto the client click event). 问题是,在IE(9)中,它不会在第一次尝试时单击服务器按钮(是的,它确实进入了客户端单击事件)。 Strangely it seems to work fine in Chrome and FF. 奇怪的是,它似乎在Chrome和FF中可以正常工作。

$(document).on('click', '.link', function (e) {
    e.preventDefault();
    var thisID = $(this).attr('href').replace('#ca', '');
    $("#hiddenField").val(thisID );
    $("#button1").submit();
});

It might be worth mentioning that the links in the div that opens in colorbox is dynamically populated. 值得一提的是,在colorbox中打开的div中的链接是动态填充的。 But this should effect it as the click events on the links are working fine. 但这应该会影响它,因为链接上的单击事件可以正常工作。

Any help would be appreciated. 任何帮助,将不胜感激。

It seems that proper form would be to trigger the button submit with a .trigger() , not a submit. 似乎适当的形式应该是使用.trigger()而非提交来触发按钮提交。 By definition, .submit() without any additional parameters mirrors .trigger('submit') which is what you'd run on a form, not a button. 根据定义,没有任何其他参数的.submit()会镜像.trigger('submit'),这是您要在表单而不是按钮上运行的内容。

For example: $("#button1").trigger("click"); 例如: $("#button1").trigger("click");

Here's the documentation on .trigger() 这是有关.trigger()文档

To me, the more logical way to submit the form would be to bypass the button altogether and .submit() the actual form itself. 对我来说,提交表单的更合乎逻辑的方法是完全绕开按钮,然后.submit()实际表单本身。 There may be reasons for why you're not doing this, but it seems more straightforward rather than relying on an element that could be "messed with" by the user. 可能有您为什么不执行此操作的原因,但是它似乎比依赖于用户可能会“迷住”的元素更直接。

e=e?e:window.event;
if($.browser.msie)
{ 
  e.returnValue=false; 
  e.cancelBubble=true;
 }
else
 {e.preventDefault();}

Try this instead: 尝试以下方法:

$(document).ready(function() {
    $('.link').click(function (e) {
        e.preventDefault();
        var thisID = $(this).attr('href').replace('#ca', '');
        $("#hiddenField").val(thisID);
        $("#button1").submit();
    }
)});

Here is [fiddle][1] for your answer. 这是[小提琴] [1]。 Its working fine on all browsers. 它在所有浏览器上都能正常工作。

I don't seem any issue with your code 我看来您的代码没有任何问题

Just moved the code in document ready function 刚刚将代码移到文档准备功能中

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

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