简体   繁体   English

如何修复javascript在新的jquery库上不起作用,而在旧版本的jquery库上起作用?

[英]How to fix javascript not working on newer jquery library but working on older version jquery library?

i have this javascript code whhich makes checkbox work as radio 我有这个JavaScript代码,使复选框可以作为收音机工作

JAVASCRIPT JAVASCRIPT

$(function() {$('input[type=checkbox]').click(function() {
        $('input[type=checkbox]').attr('checked', false);
        $(this).attr('checked', true);
    });
});

HTML HTML

<label title="cat"><input name="" type="checkbox" checked>cat</label>
<label title="dog"><input name="" type="checkbox" >dog</label>
<label title="lion"><input name="" type="checkbox" >lion</label>
<label title="fish"><input name="" type="checkbox" >fish</label>
<label title="turtle"><input name="" type="checkbox" >turtle</label>

Which was working when i was using older jquery library of version 1.8.3 but when i tried to include the newer jquery library of version 1.11.0 , its not working, 当我使用版本1.8.3的较旧jquery库但在尝试包含版本1.11.0的较新jquery库时,该方法正常工作,

how can i fix it ? 我该如何解决?

here is http://jsfiddle.net/6upmbbf0/ 这是http://jsfiddle.net/6upmbbf0/

if you change the jquery version on top left to 1.8.3 or lower it works but above 1.8.3 it doesnt. 如果将左上方的jquery版本更改为1.8.3或更低,则它可以工作,但高于1.8.3则不行。

The correct method to retrieve and update a checkbox's checked state is .prop() . 检索和更新复选框的选中状态的正确方法是.prop() The .attr() simply retrieves or updates the attribute value; .attr()只需检索或更新属性值; it does not necessarily update the underlying state of the input . 它不一定更新输入的基础状态


jQuery Core 1.9 upgrade guide mentions that behavior is changed, which explains why your code worked in jQuery < version 1.9. jQuery Core 1.9升级指南提到行为已更改,这说明了为什么您的代码在jQuery <1.9版中有效。

Use not() instead of toggling the checkbox 2 times. 使用not()而不是两次切换复选框。 If you exclude the checked checkbox in the first place, and uncheck the others, it works better. 如果首先排除选中的复选框,然后取消选中其他复选框,则效果更好。

$('input[type=checkbox]').not($(this)).prop('checked', false);

fiddle: http://jsfiddle.net/6upmbbf0/3/ 小提琴: http : //jsfiddle.net/6upmbbf0/3/

edit, added suggestion by Salman A to use prop() instead of attr() 编辑,添加了Salman A的建议以使用prop()而不是attr()

Use 'prop' Instead of 'attr' as 使用'prop'代替'attr'作为

replace code 替换代码

$(function() {$('input[type=checkbox]').click(function() {
        $('input[type=checkbox]').prop('checked', false);
        $(this).prop('checked', true);
    });
});

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

相关问题 $(…).tooltip不起作用,因为旧的jQuery UI 1.8.16库导致jQuery 1.9.2无法正常工作? 如何解决这个问题? - $(…).tooltip is not working since jQuery 1.9.2 is not working due to old jQuery UI 1.8.16 library? How to fix this? jQuery Superbox库不起作用 - jquery superbox library not working 一些jquery插件不能使用新版本的jquery库 - Some jquery plugins not working with new version of jquery library jQuery库升级到版本1.10后,代码停止工作 - code stopped working after upgrading jquery library to version 1.10 HighStock“ xRange”图表不适用于旧版本库(HighStock) - HighStock “xRange” chart is not working with older version library(HighStock) jQuery在IE 9或更旧版本中不起作用 - Jquery not working in IE 9 or older 如何修复jquery库中的&#39;jQuery is not defined&#39;错误? - how to fix 'jQuery is not defined' error in jquery library? 需要帮助以使以前可用的jQuery脚本在较新版本上运行 - Need help getting a previously working jQuery script to work on newer version 在用户脚本/greasemonkey 中,当网站已经使用旧版本时,如何使用更新版本的 jQuery? - In a userscript/greasemonkey, how do you use a newer version of jQuery when the website already uses an older version? 如何在JavaScript中嵌入jQuery库? - How to embed a jquery library in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM