javascript 在 jQuery 按钮集中以编程方式选择单选按钮使其崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18954824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:50:16  来源:igfitidea点击:

Programmatically selecting radios in jQuery buttonset makes it break down

javascriptjqueryjquery-uijqueryi-ui-buttonset

提问by Kristofer K?llsbo

I'm building an editor function on a website using a jQuery buttonset based on radio buttons. When I load the item into the editor I need to set the selected radio button and then, of course, the user can change the option and it get's saved into the database. When the item closes I want to reset the buttonset to default.

我正在使用基于单选按钮的 jQuery 按钮集在网站上构建编辑器功能。当我将项目加载到编辑器中时,我需要设置选定的单选按钮,然后,当然,用户可以更改选项并将其保存到数据库中。当项目关闭时,我想将按钮组重置为默认值。

Problem is that when I set the buttonset it works the first time I select each item then it breaks down and stops working all together.

问题是,当我设置按钮组时,它在我第一次选择每个项目时起作用,然后它会崩溃并停止一起工作。

Demo of problem: http://jsfiddle.net/kallsbo/vSmjb/

问题演示:http: //jsfiddle.net/kallsbo/vSmjb/

HTML:

HTML:

<div id="dumpSec">
    <input type="radio" name="security" id="r_public" value="public" class="rSec">
    <label for="r_public"><i class="icon-globe"></i> Public</label>
    <input type="radio" name="security" id="r_private" value="private" class="rSec">
    <label for="r_private"><i class="icon-lock"></i> Private</label>
    <input type="radio" name="security" id="r_link" value="link" class="rSec">
    <label for="r_link"><i class="icon-link"></i> Anyone with the link</label>
</div>
<div>
    If you use the buttons below you can programmatically select each of the radios in the buttonset once before it all breaks down...
</div>
<div>
     <button id="nbr1">Select Private</button><br/>
     <button id="nbr2">Select Link</button><br/>
     <button id="nbr3">Select Public</button><br/>
</div>

JavaScript:

JavaScript:

// Basic function
$("#dumpSec").buttonset();
$("#r_public").attr("checked", true);
$("#dumpSec").buttonset('refresh');

// Put in functions on the demo buttons
$( "#nbr1" )
  .button()
  .click(function( event ) {
    $("#r_private").attr("checked", true);
    $("#dumpSec").buttonset('refresh');
  });

$( "#nbr2" )
  .button()
  .click(function( event ) {
    $("#r_link").attr("checked", true);
    $("#dumpSec").buttonset('refresh');
  });

$( "#nbr3" )
  .button()
  .click(function( event ) {
    $("#r_public").attr("checked", true);
    $("#dumpSec").buttonset('refresh');
  });

I have tried a number of things like trying to trigger the click function of the label for each radio button and so on but I can't find anything that works. Any input is appreciated!

我尝试了很多方法,例如尝试为每个单选按钮触发标签的点击功能等等,但我找不到任何有效的方法。任何输入表示赞赏!

回答by Arun P Johny

You need to use .prop()instead of .attr()to set the checked status

您需要使用.prop()而不是.attr()来设置选中状态

$("#r_link").prop("checked", true);

Demo: Fiddle

演示:小提琴

Read: Attributes vs Properties

阅读:属性与属性

回答by Gena Moroz

http://jsfiddle.net/vSmjb/2/

http://jsfiddle.net/vSmjb/2/

you can trigger click(), to make it work

你可以触发click(),使其工作

  $("#r_private").click()