对象没有方法'追加'-需要快速帮助

Object ... has no method 'append' -Need help quick

本文关键字:#39- 帮助 追加 有方法 对象      更新时间:2023-09-26

我目前正试图编写一个函数,通过勾选复选框将唯一id动态应用于列表项,尽管我遇到了一个问题,当将唯一id附加到列表项时,我在控制台中收到以下错误消息:

Uncaught TypeError: Object SpTU 4, 183:false<br /> has no method 'append'

以下是导致错误的代码:

strng += name+":"+val+"<br />";
var i = 1;
strng.append($({ type: "text", id:+i }));

我很快就需要帮助,所以任何帮助都将不胜感激!提前感谢

-------编辑----------这是整个函数,所以它更容易理解,我是新编程的,对它来说可能非常混乱和不专业。

var dataToShow = {};
function check(tickbox){
  dataToShow[tickbox.value] = tickbox.checked == true;
  showDataOnScreen(dataToShow);

function showDataOnScreen(dataToShow){
var $strng = "";
jQuery.each(dataToShow,function(name,val){
    $strng += name+":"+val+"<br />";
    var i = 1;
    $strng.append($({ type: "text", id:+i }));
    });
jQuery("#list").html(strng);

假设您使用的是JQUERY ,请确保strng是一个JQUERY对象

如果您正试图构建一个已检查项目的列表,我会这样做。

// create an array to store the checked items
var checkedItems = new Array();
// loop through the checked check boxes contained in the list element
// and add them to the array
$("#list :checked").each(function (){
    var item = { name: $(this).attr("name"), value: $(this).val() };
    checkedItems.push(item);
});

或者,更好的是,如果您计划在不同的元素中显示结果,您可以剪切数组。

// loop through the checked check boxes contained in the list element
// and add them to a different element
$("#list :checked").each(function (){
    var item = "<div>"+ $(this).attr("name") + ": " + $(this).val() + "</div>";
    $("#selectedList").append(item);
});

下面是一个关于jsFiddle的工作示例。

参考文献:

  • 阵列:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array
  • jQuery each():http://api.jquery.com/jQuery.each/
  • jQuery:选中的选择器:http://api.jquery.com/checked-selector/
  • jQuery append():http://api.jquery.com/append/