简体   繁体   English

通过javascript的imageSwap onClick无法触发

[英]imageSwap onClick via javascript not firing

As can be demonstrated in the following example 以下示例所示

A script that swaps images (generated via a Rails <%= javascript_tag do %> tag) 交换图像的脚本(通过Rails <%= javascript_tag do %>标签生成)

<script>
//<![CDATA[

$("input[type=radio]").on("change", function() {
      var Frame_image = $("input[name=Frame]:checked").val();
      $("#Frame").attr("src", "/Mirra_2/small/Frame_" +  Frame_image  + ".png");
})

//]]>
</script>

based on source (snippet) 根据来源(摘要)

<table> <tr>
  <td style='vertical-align: top;'>
      <label><input type="radio" class='imagepick' id="Frame_6K8" name="Frame" value="6K8"><img src="/assets/Mirra_2/colours/6K8-f0d0fc886240ff430ef97b3f98ac23bf26f290b772bcb840460549cd61d15028.jpg" />Studio White / H-Alloy</label>
  </td>
  <td style='vertical-align: top;'>
    <label><input type="radio" class='imagepick' id="Frame_6K7" name="Frame" value="6K7"><img src="/assets/Mirra_2/colours/6K7-8052fe7aa605d525981035af0c1367d1d3808390b63bb29437f515cc63b58e8c.jpg" />Graphite / H-Alloy</label>
  </td> [...]

note the initial images are in the first section of the body 请注意初始图像在主体的第一部分中

The javascript is not generating any errors in the console. javascript在控制台中未生成任何错误。 The images to swap for exist. 要交换的图像存在。 Yet, clicking upon choices does not lead to the image swap. 但是,单击选项不会导致图像交换。

Why is that? 这是为什么?

With problems like these, take the time to debug your code and see what is going wrong and verify that you are getting the results you expect at each step. 对于此类问题,请花一些时间调试代码,看看问题出在哪里,并验证您在每一步中都获得了预期的结果。

If you had added console.log() calls like these, you would have quickly discovered that you have no element with the Frame id. 如果添加了像这样的console.log()调用,您将很快发现您没有带有Frame id的元素。

$("input[type=radio]").on("change", function() {

      var Frame_image = $("input[name=Frame]:checked").val();
      console.log(Frame_image);
      console.log("#Frame");
      $("#Frame").attr("src", "/Mirra_2/small/Frame_" +  Frame_image  + ".png");
})

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

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