【问题标题】:How to pass select option values from php code to Jquery?如何将选择选项值从 php 代码传递给 Jquery?
【发布时间】:2019-12-08 16:15:23
【问题描述】:

单击添加行按钮时,新行将添加到特定表中。所以我需要添加一个带有 php 选项值的选择选项。

如何将这个 php 值传递给 jQuery?

jQuery 函数 我需要在 rowData.push(''); 中显示选择选项;

  $('.dt-add').each(function () {
        var whichtable = $(this).parents('form').attr('data-id');
        $(this).on('click', function(evt){
            //Create some data and insert it
            var rowData = [];
            var table = $('#teammembertable' + whichtable).DataTable();
//          rowData.push('');

            rowData.push('<select class="form-control addstafftype" id="addstafftype" name="addstafftype"><option value="">Select</option><option value="Leader">Leader</option><option value="Technician">Technician</option></select');
            rowData.push('<button type="button" data-id='+ whichtable +' class="btn-xs dt-delete dt-deletes"><i style="font-size:10px" class="fa">&#xf00d;</i></button>');
            table.row.add(rowData).draw( false );
        });
    });

PHP 代码

$dataadd_team_memb = array(
        'team_id' => $id,
        'Staff_id' => $this->input->post('getaddstaffname'),
        'Staff_type' => $this->input->post('getaddstafftype'),
        'status' => "active"
    );


    $insert_id = 0;
    if ($this->db->insert("team_members", $data)) {
        $insert_id = $this->db->insert_id();
    }

【问题讨论】:

  • select 元素放入form 并将该表单提交到PHP 页面。

标签: javascript php jquery codeigniter


【解决方案1】:
$('.dt-add').each(function () {
    var whichtable = $(this).parents('form').attr('data-id');
    $(this).on('click', function(evt){
        var rowData = [];
        var table = $('#teammembertable' + whichtable).DataTable();
        rowData.push('<select class="form-control addstafftype" id="addstafftype" name="addstafftype">'+
            '<option value="">Select</option>'+
            '<?php foreach($selectallstaff as $staffname){ ?>'+
                '<option value="<?php $staffname["Staff_id"]; ?>"><?php $staffname["Staff_name"]; ?></option>'+
            '<?php } ?>'+
            '</select');
        rowData.push('<button type="button" data-id='+ whichtable +' class="btn-xs dt-delete dt-deletes"><i style="font-size:10px" class="fa">&#xf00d;</i></button>');
        table.row.add(rowData).draw( false );
    });
});

【讨论】:

  • 当我给 = $staffname["Staff_id"]; ?>
  • 还记得&lt;?= $staffname["Staff_name"]; ?&gt;
  • 是的,当你在 PHP 上使用最新版本时。
  • for($i=0;$i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多