简体   繁体   English

我无法将foreach选择选项放在JS中

[英]I can't put my foreach Select Option in JS

Hi can someone help me put my foreach result in my JS add row function . 嗨,有人可以帮我把我的foreach结果放到我的JS add row函数中

I'm Using Codeigniter that will pass the data from controller to view. 我正在使用Codeigniter,它将从控制器传递数据到视图。

So I have SELECT in VIEW that will give results of my foreach particulars in my OPTIONS . 所以我在VIEW中有SELECT ,它将在我的OPTIONS中给出我的foreach细节的结果。

<td class="form-group">
    <select class="form-control" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>">
        <option value="">Select Particulars</option>

        <?php foreach ($particularsData as $particulars)
        {
        ?>

        // FOR EACH RESULTS INSIDE THE OPTIONS
        <option value="<?php echo $particulars['feetype_id']; ?>"> <?php echo $particulars['feetype_name']; ?> </option>

        <?php 
        } 
        ?>
    </select>                     
</td>

So the the Modal Form Select Option will look like this. 因此,模态形式选择选项将如下所示。 在此处输入图片说明

Now let's move to my Add Row Function in my JS where if you click Add Row Button on the top right side it will add new identical rows. 现在,让我们转到我的JS中的“ 添加行功能” ,如果您单击右上角的“ 添加行”按钮 ,它将添加新的相同行。

function addPaymentRow()
{
var tableLength = $("#addSubPaymentTable tbody tr").length;

var tableRow;
var arrayNumber;
var count;

if(tableLength > 0) {       
    tableRow = $("#addSubPaymentTable tbody tr:last").attr('id');
    arrayNumber = $("#addSubPaymentTable tbody tr:last").attr('class');
    count = tableRow.substring(3);  
    count = Number(count) + 1;
    arrayNumber = Number(arrayNumber) + 1;                  
} else {
    // no table row
    count = 1;
    arrayNumber = 0;

}

// I NEED HELP HERE, I CAN ADD ROWS with no foreach value just plain input type, HOW CAN I ADD VALUES HERE FROM FOR EACH                  
var tr = '<tr id="row'+count+'" class="'+arrayNumber+' appended-exp-row">'+                                 
    '<td class="form-group">'+
        '<select class="form-control" name="subparticulars['+count+']" id="subparticulars'+count+'">'+
        '<option value="">Select Particulars</option>'+
        '</select>'+                        
    '</td>'+

    '<td class="form-group">'+
        '<input type="text" class="form-control" name="subpaymentbalance['+count+']" id="subpaymentbalance'+count+'"/>'+                        
    '</td>'+

    '<td class="form-group">'+          
        '<input type="text" class="form-control" name="subpaymentamount['+count+']" id="subpaymentamount'+count+'" onkeyup="calculateTotalAmount()" placeholder="Payment Amount" />'+                                   
    '</td>'+

    '<td class="form-group">'+
        '<button type="button" class="btn btn-danger btn-sm" onclick="removePaymentRow('+count+')">Remove</button>'+
    '</td>'+
'</tr>';

if(tableLength > 0) {                           
    $("#addSubPaymentTable tbody tr:last").after(tr);
} else {                
    $("#addSubPaymentTable tbody").append(tr);
}               
}

The result of added row will look like this. 添加行结果将如下所示。 在此处输入图片说明

Can someone help me populate my options in my JS coming from my foreach like in my VIEW . 有人可以帮我从我的foreach中 填充我的JS 选项 ,就像在VIEW中一样

Try this: $('.particulars option').clone() will give you all the options of your main select then you just need to append that to your newly created select. 试试这个: $('.particulars option').clone()将为您提供主选择的所有选项,然后您只需将其附加到新创建的选择上即可。

<td class="form-group">
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>">
    <option value="">Select Particulars</option>

    <?php foreach ($particularsData as $particulars)
    {
    ?>

    // FOR EACH RESULTS INSIDE THE OPTIONS
    <option value="<?php echo $particulars['feetype_id']; ?>"> <?php echo $particulars['feetype_name']; ?> </option>

    <?php 
    } 
    ?>
</select>                     

 function addPaymentRow()
    {
        var tableLength = $("#addSubPaymentTable tbody tr").length;

        var tableRow;
        var arrayNumber;
        var count;

        if (tableLength > 0) {
            tableRow = $("#addSubPaymentTable tbody tr:last").attr('id');
            arrayNumber = $("#addSubPaymentTable tbody tr:last").attr('class');
            count = tableRow.substring(3);
            count = Number(count) + 1;
            arrayNumber = Number(arrayNumber) + 1;
        } else {
            // no table row
            count = 1;
            arrayNumber = 0;

        }

// I NEED HELP HERE, I CAN ADD ROWS with no foreach value just plain input type, HOW CAN I ADD VALUES HERE FROM FOR EACH                  
        var tr = '<tr id="row' + count + '" class="' + arrayNumber + ' appended-exp-row">' +
                '<td class="form-group">' +
                '<select class="form-control" name="subparticulars[' + count + ']" id="subparticulars' + count + '"></select>' +
                '</td>' +
                '<td class="form-group">' +
                '<input type="text" class="form-control" name="subpaymentbalance[' + count + ']" id="subpaymentbalance' + count + '"/>' +
                '</td>' +
                '<td class="form-group">' +
                '<input type="text" class="form-control" name="subpaymentamount[' + count + ']" id="subpaymentamount' + count + '" onkeyup="calculateTotalAmount()" placeholder="Payment Amount" />' +
                '</td>' +
                '<td class="form-group">' +
                '<button type="button" class="btn btn-danger btn-sm" onclick="removePaymentRow(' + count + ')">Remove</button>' +
                '</td>' +
                '</tr>';

        if (tableLength > 0) {
            $("#addSubPaymentTable tbody tr:last").after(tr);
        } else {
            $("#addSubPaymentTable tbody").append(tr);
        }

        $('.particulars option').clone().appendTo('#subparticulars' + count);
    }

暂无
暂无

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

相关问题 我可以在选择选项旁边有一个按钮吗? 如果不能,我可以将按钮放在外面并与选择中的选项连接吗? - can I have a button next to the select option? if can't, can I put the button outside and connect with the option in the select? 我无法在JavaScript中显示“选择”选项 - I can't display the option of select in javascript 为什么不能在select中选择选项? - Why can't I choose option in select? 当我有一个隐藏的字段(如果选择了一个选项时会显示)时,我无法获得select元素的值 - I can't get the value of my select element when i have a hidden field that is revealed if an option is selected 我不能在我的 js 文件中放置两个或更多 svg 形状并让它们显示在我的网站上 - I can't put two or more svg shapes in my js file and have them show on my website 为什么我的 select 用 JS 创建时不会出现选项? - Why won't my select appear with an option when created with JS? 如何使用CSS和JS编写的多选代码仅选择一个选项? 目前,我可以同时选择 - How do I make my multiple choice code written in CSS & JS to only select one option? Currently I can select both 为什么不能使用URL参数设置选择选项的值? - Why can't I set the value of a select option with my URL parameters? 为什么我不能在我的JS计算中添加一个点 - Why can't I put more then one dot in my JS calc 如何将我的选择放在我的开关旁边? - How can I do to put my select next to my switch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM