首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >checkboxradio中的刷新方法不起作用

checkboxradio中的刷新方法不起作用
EN

Stack Overflow用户
提问于 2019-09-15 19:31:47
回答 2查看 124关注 0票数 0

我有一个单选按钮的列表,当单击add按钮时,在单选按钮中添加新项,但刷新方法不起作用,并给我错误:

未捕获的错误:无法在初始化之前调用checkboxradio上的方法;尝试调用方法“”refresh“”

代码语言:javascript
复制
at Function.error (VM80 jquery-1.12.4.js:253)
at HTMLInputElement.<anonymous> (VM81 jquery-ui.js:246)
at Function.each (VM80 jquery-1.12.4.js:370)
at jQuery.fn.init.each (VM80 jquery-1.12.4.js:137)
at jQuery.fn.init.$.fn.<computed> [as checkboxradio] (VM81 jquery-ui.js:236)
at HTMLButtonElement.<anonymous> (tryit.asp?filename=tryjquery_event_bind_ref:11)
at HTMLButtonElement.dispatch (VM80 jquery-1.12.4.js:5226)
at HTMLButtonElement.elemData.handle (VM80 jquery-1.12.4.js:4878)

下面是我的代码:

代码语言:javascript
复制
$("input").checkboxradio();

var i = 4;
$(".add").click(function(e) {
  $("fieldset:first").append(' <label for="radio-' + i + '">London</label><input type="radio" name="radio-1" id="radio-' + i + '">');
  i++;
});
$(".refresh").click(function(e) {
  $("input").checkboxradio("refresh");
});
代码语言:javascript
复制
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<h2>Radio Group</h2>
<fieldset>
  <legend>Select a Location: </legend>
  <label for="radio-1">New York</label>
  <input type="radio" name="radio-1" id="radio-1">
  <label for="radio-2">Paris</label>
  <input type="radio" name="radio-1" id="radio-2">
  <label for="radio-3">London</label>
  <input type="radio" name="radio-1" id="radio-3">
</fieldset>
<button class="add">add</button>
<button class="refresh">refresh</button>

EN

回答 2

Stack Overflow用户

发布于 2019-09-15 19:54:01

问题是,当您单击add按钮时,您正在创建一个新的单选输入,但是您没有使用checkboxradio对其进行初始化。

因此,当您在最后一个输入上调用刷新方法时,它会导致您看到的错误。

如果要在所有输入上调用刷新,则应首先初始化正在创建的复选框:

代码语言:javascript
复制
$("input").checkboxradio();

var i = 4;
$(".add").click(function(e) {
    var id = 'radio-' + i;
    $("fieldset:first").append(' <label for="' + id + '">London</label><input type="radio" name="radio-1" id="' + id + '">');
    $('#' + id).checkboxradio();
    i++;
});
$(".refresh").click(function(e) {
    $("input").checkboxradio("refresh");
});
票数 1
EN

Stack Overflow用户

发布于 2019-09-15 19:58:29

您只需在新添加的标签和输入单选框中添加一个单独的类,并在jQuery代码中稍作更改。请参考下面的解决方案。只需添加新的类名作为"extras“,并使用删除内置属性函数。

代码语言:javascript
复制
$("input").checkboxradio();

var i = 4;
$(".add").click(function(e) {
  $("fieldset:first").append(' <label class="extras" for="radio-' + i + '">London</label><input type="radio" class="extras" name="radio-1" id="radio-' + i + '">');
  i++;
});
$(".refresh").click(function(e) {
 // $("input").checkboxradio("refresh");
  $('.extras').remove();
});
代码语言:javascript
复制
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<h2>Radio Group</h2>
<fieldset>
  <legend>Select a Location: </legend>
  <label for="radio-1">New York</label>
  <input type="radio" name="radio-1" id="radio-1">
  <label for="radio-2">Paris</label>
  <input type="radio" name="radio-1" id="radio-2">
  <label for="radio-3">London</label>
  <input type="radio" name="radio-1" id="radio-3">
</fieldset>
<button class="add">add</button>
<button class="refresh">refresh</button>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57943698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档