Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将jquery克隆对象关联到字符串_Jquery_Jquery Select2 - Fatal编程技术网

如何将jquery克隆对象关联到字符串

如何将jquery克隆对象关联到字符串,jquery,jquery-select2,Jquery,Jquery Select2,我已经克隆了select2框,现在我想用字符串连接克隆的对象,然后将其附加到目标字段。 html是 <select data-placeholder="Select a billable item" name="nvoice_billable_ITEMID" id="invoice_billable_ITEMID" class="invoice_billable_ITEMID"> <option value=""></option> <o

我已经克隆了select2框,现在我想用字符串连接克隆的对象,然后将其附加到目标字段。 html是

<select data-placeholder="Select a billable item" name="nvoice_billable_ITEMID" id="invoice_billable_ITEMID" class="invoice_billable_ITEMID">
    <option value=""></option>
    <option value="35089">Initial</option>
    <option value="35088">Standard</option>
</select>
在函数调用add_fields时,我创建了克隆对象,并在将其与字符串连接后将其附加到目标元素。克隆对象创建得很好,但在追加后,字符串将显示
[object object]
,以代替带有in sting的克隆对象。如何将对象与字符串连接,使选择框可见

//on call add the field for item
function add_fields(item_type){
    if(item_type == "Billable"){
        //   create the clone of select2 box
        var newBillableSelect = select_billable.clone();
        //function for concate the select2 box with string and append it
        appendRow(newBillableSelect);
        //initilizing the new select2 box
        newBillableSelect.select2();   
    }else if(item_type == "Product"){
        var newProductSelect = select_product.clone();
        appendRow(newProductSelect);
        newProductSelect.select2();  
    }
}
function appendRow(selectBox){
    var tr = '<tr class="fields_group" id="fields_group_ITEMID">'+
        '<td>'+ selectBox.html() +'</td>'+
        '<td>$<input type="text" style="width: 60px" size="30" name="unit_price[]" id="unit_price"></td>'+
        '<td><input type="text" style="width: 45px" size="30" name="quantity[]" id="quantity"></td>'+
        '<td><input type="hidden" name="tax_id" id="tax_id"><label class="tax_name">N/A</label><input type="hidden" name="tax_rate[]" id="tax_rate"></td>'+
        '<td>$<input type="text" value="0.00" size="10" readonly="readonly" name="net_price" id="net_price" class="read_only_text right_align_text" style="background-color: #fff;border:none;width: 100px;box-shadow:none "></td>'+
        '<td><input type="hidden" value="false" name="net_price[]" id="net_price"><a tabindex="-1" onclick="remove_invoice_item_fields(this); return false;" href="#" class="link_red link_small">remove</a></td>'+
        '</tr>';
    $('#items_tbody').append(tr);
}
//随叫随到为项目添加字段
函数添加字段(项目类型){
如果(项目类型=“可计费”){
//创建select2框的克隆
var newBillableSelect=select_billable.clone();
//函数用于用字符串连接select2框并将其追加
appendRow(newbilableselect);
//初始化新的select2框
newBillableSelect.select2();
}否则如果(项目类型=“产品”){
var newProductSelect=select_product.clone();
appendRow(newProductSelect);
newProductSelect.select2();
}
}
函数追加行(选择框){
var tr=''+
“”+selectBox.html()+“”+
'$'+
''+
“不适用”+
'$'+
''+
'';
$('items_tbody')。追加(tr);
}

好了,你快到了。
.html()
方法将返回html字符串,但它将返回元素的内部html(即元素的内容/子元素)。为了获取元素本身的html,您可以将select包装在一个div中,并将该div与jQuery选择器匹配,或者使用一个临时元素,例如:

'<td>'+ $('<div>').append(selectBox).html() +'</td>'
“”+$(“”).append(selectBox.html()++”

好了,你快到了。
.html()
方法将返回html字符串,但它将返回元素的内部html(即元素的内容/子元素)。为了获取元素本身的html,您可以将select包装在一个div中,并将该div与jQuery选择器匹配,或者使用一个临时元素,例如:

'<td>'+ $('<div>').append(selectBox).html() +'</td>'
“”+$(“”).append(selectBox.html()++”

您确定测试时使用了
html()
部分吗?我想不出这种方法会返回对象的场景。另外,
$('.invoice\u bilable\u ITEMID')
选择器匹配的是什么?如果它匹配一个select元素,那么对它调用
html()
将返回嵌套的选项元素,这些元素在td中不是有效的html。我已经编辑了post$('.invoice\u billable\u ITEMID')选择器用于select2。我已经编辑了post$('.invoice\u billable\u ITEMID')选择器。它是用于选择2框的。而且你是对的,它没有html()部分,但我只是把它放在试图获取对象html的地方,但它只打印选择框的选项值。知道如何将对象/克隆对象附加到字符串中吗?你确定在测试时有
html()
部分吗?我想不出这种方法会返回对象的场景。另外,
$('.invoice\u bilable\u ITEMID')
选择器匹配的是什么?如果它匹配一个select元素,那么对它调用
html()
将返回嵌套的选项元素,这些元素在td中不是有效的html。我已经编辑了post$('.invoice\u billable\u ITEMID')选择器用于select2。我已经编辑了post$('.invoice\u billable\u ITEMID')选择器。它是用于选择2框的。而且你是对的,它没有html()部分,但我只是把它放在试图获取对象html的地方,但它只打印选择框的选项值。知道如何将对象/克隆对象附加到字符串中吗?很好的解决方案。动态地附加到div并获取innerHTML。谢谢,很好的解决方案。动态地附加到div并获取innerHTML。谢谢