【问题标题】:Why isn't this working on Internet Explorer? [jQuery, radio, selectors]为什么这在 Internet Explorer 上不起作用? [jQuery,收音机,选择器]
【发布时间】:2011-05-21 15:04:09
【问题描述】:

我正在尝试调试在任何版本的 Internet Explorer 上都不起作用的东西。

代码如下:

<div id="sondage">
    <input type="radio" name="reponse" value="oui" id="oui">
    <label for="oui">Oui</label>

    <input type="radio" name="reponse" value="non" id="non">
    <label for="non">Non</label>
</div>

<script type="text/javascript" charset="utf-8">
$(function(){

    $('#oui, #non').click(function(){
        reponse = $('input[name=reponse]:checked').val();
        sondage_id = <?php echo $sondage->id ?>;

        $.ajax({
            type: "GET",
            url: "<?php echo url_for('@sondage_repondre') ?>",
            data: "reponse="+reponse+"&id="+sondage_id,
            success: function(msg){
                resultat = msg.split('|');

                if (resultat[0] == "true") {
                    $('#sondage_message').html("<?php echo __('Merci.') ?>");
                } else {
                    $('#sondage_message').html("<?php echo __('Désolé, vous avez déjà voté pour ce sondage. Merci.') ?>");
                }
                $('#sondage').html(resultat[1]);
            }
        });

    });

});
</script>

错误发生在该行 (reponse = $('input[name=reponse]:checked').val();)。

你知道发生了什么吗?

谢谢!

编辑:如问:错误是:

Line 511, Char 7, Object doesn't support this property or method.

这是完整的输出:http://pastie.org/1355610

再次感谢!

【问题讨论】:

  • 你确定那是导致错误的行吗?我不相信 IE 的行号,但您可以通过硬编码该值来验证它,看看它是否有效。你得到什么错误?您能否使用呈现的代码(“查看源代码”)而不是实际源代码来更新您的问题? &lt;? php echo 不是 IE 所看到的,所以这对我们没有多大帮助。例如,如果$sondage-&gt;id 产生一个字符串或一个空值,这将在任何浏览器上中断。
  • 你完全正确,我更新了问题。谢谢:)
  • 您从 IE 得到的错误信息是什么?
  • 第 511 行,字符 7,对象不支持此属性或方法。

标签: javascript jquery internet-explorer jquery-selectors


【解决方案1】:

您在其中缺少var,应该是:

var reponse = $('input[name=reponse]:checked').val();

您的其他变量也是如此...始终使用var 声明它们,无论它们在哪里,都不要依赖于并非总是允许的隐式全局定义。

【讨论】:

  • 但是隐式声明变量是完全有效的。这不应该导致脚本废话。诚然,更好的编码实践来显式声明变量。
  • @Metagrapher - 它不是有效,在某些情况下它是允许的。在这两种情况下,这都是不好的做法。
  • @Pointy:我不知道!但看起来你是对的west-wind.com/weblog/posts/677442.aspx
  • 完全正确,该死的我几乎永远不会忘记那些东西......我的错。谢谢!
猜你喜欢
  • 2011-01-25
  • 2010-09-07
  • 1970-01-01
  • 2015-12-08
  • 2015-05-13
  • 2020-05-10
  • 2013-07-31
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多