简体   繁体   English

jQuery .trigger单击事件在IE下不起作用

[英]JQuery .trigger click event not working under IE

I am using the following code to route click events on an img tag to an input radio below it. 我正在使用以下代码将img标签上的点击事件路由到其下方的输入单选按钮。 The code works perfectly on Chrome and other browsers, but on IE (specifically IE 11) I must double click to get the radio selected instead of just single click to get the radio input selected. 该代码可以在Chrome和其他浏览器上完美运行,但是在IE(特别是IE 11)上,我必须双击才能选择单选,而不仅仅是单击才能选择单选。 Can someone please tell me what I am doing wrong, missing here? 有人可以告诉我我做错了什么,想念这里吗? Thanks 谢谢

<script type="text/javascript">
   $(document).ready(function() {
       $('#Img1').click(function() {
          $('#radio1').trigger('click');
       });
   });
</script> 




      <div class="imagesPrev col four center">
        <label class="label_radio images" for="radio1">
      <div class="labelText">
            <img id="Img1"src="image1.gif" alt="Image 1" />
            <input type="radio" id="radio1" name="radio1" value="image1"/>      
      </div>
      <div style="height:10px;"></div>
             Image Title <br/>
        </label>
      </div>

Notes: - I also noticed that I don't have to double click as normal double click, but it has to be two clicks. 注意:-我也注意到我不必像普通双击一样双击,但必须单击两次。 Meaning one click then I can wait for like 10-15 seconds then do the 2nd click to get the click event routed to the radio input. 意思是单击一次,然后我可以等待10-15秒钟,然后单击第二次,以将点击事件路由到单选输入。

http://jsfiddle.net/89wTk/ http://jsfiddle.net/89wTk/

You should use .prop(); 您应该使用.prop(); when dealing with checkbox/radio inputs. 处理复选框/无线电输入时。

   $(document).ready(function() {
       $('#Img1').click(function() {
           var checkBox = $("#radio1");
           checkBox.prop("checked", !checkBox.prop("checked"));
       });
   });

Have you tried using a label tag with a for attribute for this feature instead, this could solve your problem and be more browser compatible. 您是否尝试过使用带有for属性的label标记来代替此功能,这可以解决您的问题并与浏览器更加兼容。

<label for="radio1"><img id="Img1"src="image1.gif" alt="Image 1" /></label>
<input type="radio" id="radio1" name="radio1" value="image1"/>

I can understand if this doesn't achieve what you need, but using this method should work using HTML markup instead of jQuery 我可以理解这是否无法满足您的需求,但是使用此方法应该可以使用HTML标记而不是jQuery

relatively horrible jsfiddle demoing this: http://jsfiddle.net/andyface/d25KS/ 相对可怕的jsfiddle进行了演示: http : //jsfiddle.net/andyface/d25KS/

I remember that some version of IE don't support clicking objects other than links or buttons :( 我记得某些版本的IE不支持单击链接或按钮以外的对象:(

Perhaps try using: 也许尝试使用:

$("#radio1").checked(true);

or 要么

$("#radio1").selected(true);

as a work around 作为解决

Just simple, you don't have to use any Jquery for this, just keep everything inside the label : 很简单,您不必为此使用任何Jquery,只需将所有内容保留在label

<label for="radio_btn">
   <img id="img"src="image1.gif" alt="Image here" />
   <input type="radio" id="radio_btn" name="radio1" value="image1"/>
</label>

Working here: http://jsfiddle.net/fals/3phf4/ 在这里工作: http : //jsfiddle.net/fals/3phf4/

Your code example works fine in IE11 (desktop and metro). 您的代码示例在IE11(台式机和Metro)中可以正常工作。 Is it possible that there is another click event on the page that is capturing and preventing the click event on the image? 页面上是否可能有另一个单击事件正在捕获并阻止图像上的单击事件? Maybe something is causing the page to lose focus (first click gets window focus second clicks the image)? 也许是某种原因导致页面失去焦点(第一次单击获得窗口焦点,第二次单击图像)? Try putting an alert in the click function to see if the click is getting registered. 尝试在点击功能中添加警报,以查看点击是否已注册。 If that's not the issue, try running the body of the function in the console to see if that is the issue. 如果不是问题,请尝试在控制台中运行该函数的主体,以查看是否是问题所在。 You might try other ways to trigger the event, as suggested by others. 您可以尝试其他方式触发事件,如其他人所建议。 Or try the jQuery .triggerHandler("click") method. 或尝试使用jQuery .triggerHandler(“ click”)方法。

I think your problem may be with your markup. 我认为您的问题可能出在您的标记上。 You have your image and click events inside a label. 您将图像放在标签内并单击事件。

According to w3schools: "...if the user clicks on the text within the element, it toggles the control." 根据w3schools的说法:“ ...如果用户单击元素内的文本,则会切换控件。” http://www.w3schools.com/tags/tag_label.asp http://www.w3schools.com/tags/tag_label.asp

That toggle is perhaps messing with your javascript. 该切换可能会使您的JavaScript混乱。 Try moving the markup outside of the label and see if that helps. 尝试将标记移到标签外,看看是否有帮助。

That's an easy one :). 这是一个简单的:)。

The #img1 is encapsulated inside the #img1封装在

<label class="label_radio images" for="radio1">

So, without the jQuery part it's already working. 因此,没有jQuery部分,它已经可以工作了。 Now when having the jQuery part, a single click will become a double click: 1) 'for' element of label_radio 2) the trigger in jQuery part 现在,当拥有jQuery部分时,单击将变成双击:1)label_radio的“ for”元素2)jQuery部分中的触发器

So, for x-browser, you should not wrap the img inside the label. 因此,对于x浏览器,您不应将img包装在标签内。 Or try to cancel the event in $('#Img1').click(function(event) { ... 或者尝试取消$('#Img1')。click(function(event){...

You have both your img and radio wrapped in label . 您的imgradio包裹在label HTML's default behavior is that click on label triggers radio/checkbox inside. HTML的默认行为是单击标签会触发内部的单选/复选框。

There are two ways you can solve your problem: 有两种方法可以解决问题:

  1. Remove javascript function altogether. 完全删除javascript函数。
  2. If there's additional reason for javascript, change your html markup, and move radio outside of the label. 如果还有其他原因需要使用javascript,请更改您的html标记,然后将radio移到标签之外。 Also remove for attribute, cause it triggers radio selection. 同时删除for属性,因为它触发无线选择。 Something like this: 像这样:

     <div class="imagesPrev col four center"> <label class="label_radio images"> <div class="labelText"> <img id="Img1"src="image1.gif" alt="Image 1" /> </div> </label> <input type="radio" id="radio1" name="radio1" value="image1"/> <div style="height:10px;"></div> Image Title </div> 
<script type="text/javascript">
   $(document).on('click','.Img1',function()
   {
      var checkBox = $(".radio1");
      checkBox.prop("checked", !checkBox.prop("checked"));
   });
</script>

<img class="Img1" src="image1.gif" alt="Image 1" />
<input type="radio" class="radio1" name="radio1" value="image1"/>  

I am facing the same issue and its very weird .... the workaround that is working for me is that instead of using the trigger function .. put the code which is executed in the trigger click event in a javascript function and call that function instead of trigger event ... example is given below. 我正面临着同样的问题,这很奇怪..对我而言,解决方法是不使用触发函数..将在触发click事件中执行的代码放在javascript函数中并调用该函数而不是触发事件...下面给出示例。 For this example i will use an alert in case of a click on the element with id radio1. 对于此示例,在单击ID为radio1的元素时,我将使用警报。

Event definition
$('#radio1').on('click',function () {
alert("Hi");
})
So Instead of using $('#radio1').trigger('click'); we can put the alert in a javascript function and call the javascript function where ever i am calling the trigger click event.

Javascript function 

function triggerClick(){
alert("Hi")
}

and just call this function(triggerClick()) where ever you are using $('#radio1').trigger('click');

Please let me know if this is helpfull 请告诉我这是否有帮助

Try to use .click() instead of .trigger('click'): 尝试使用.click ()而不是.trigger('click'):

<script> $(document).ready(function() { $('#Img1').click(function() { $('#radio1').click(); }); }); </script>

it should work, see here for more info 它应该可以工作,请参见此处以获取更多信息

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

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