【问题标题】:How to get the value from the array如何从数组中获取值
【发布时间】:2020-04-06 03:08:09
【问题描述】:

这次的问题是我总是得到我从函数中取出的数组的索引,我对编程还是很初学者,所以请指导我

函数arr_month():

    function arr_month()
    {
        $month_names = array(
            1=>"Januari",2=>"Februari",3=>"Maret",4=>"April",5=>"Mei",6=>"Juni",
            7=>"Juli",8=>"Agustus",9=>"September",10=>"Oktober",11=>"Nopember",12=>"Desember"
        );
        return $month_names;
    }

这是我的控制器上的索引:

    function index()
        {
            if($this->processData["is_view"] == false) { show_404("forbidden access ".$this->page_name, false); }
            $this->page_title_attr  .= $this->lang->line('datalist');

            $this->Tbl_asset_penyusutan_asset->defaultFieldsJoin();

            $arr_controller = array(
                "arr_fields_alias" => $this->Tbl_asset_penyusutan_asset->arr_fields_alias,
            );      

            $arr_viewElements = $this->_loadFormInput();
            $arr_permittedOperation = $this->_permittedOperation();
            $arr_controller['arr_tahun'] = $this->Tbl_asset_penyusutan_asset->tahunPerolehan();;
            $arr_controller['arr_bulan'] = arr_month();
            // $arr_tahun = $this->Tbl_asset_penyusatan_asset->tahunPerolehan()
            $arr_controller = array_merge($arr_controller ,$arr_permittedOperation);

            $this->load->view($this->page_name."_view",$arr_controller,$arr_viewElements);
        }

当我在视图中调用时:

<div class="col-lg-2 col-xs-12 col-sm-3">
					<div class="input-group">
						<label>Bulan
							<?php echo form_dropdown("bulan", $arr_bulan, set_value("bulan"), 'id="bulan" class="form-control chzn-select" data-placeholder="Pilih"  required');?>
							<?php echo form_error('bulan','<span class="label label-danger">','</span>'); ?>
						</label>
					</div>
				</div>

当我使用 jquery 检查值时,出来的结果不是一个月,而是我正在调用的变量的索引数组

【问题讨论】:

  • 您是否尝试过将月份声明为普通数组,例如 ['jan', 'feb'...] 而无需手动设置密钥?

标签: php jquery codeigniter


【解决方案1】:
Replace Line
$arr_controller['arr_bulan'] = arr_month();

to

$arr_controller['arr_bulan'] = array_values(arr_month());

【讨论】:

  • 它不起作用,当我使用 console.log 检查值仍然是数组的索引 $("#bulan").click(function () { console.log($("#bulan" ).val()); });
【解决方案2】:

代码没有错。您使用 Jquery 从选择中检索值的方式是问题所在。

你的选择是这样的

<select id="bulan">
   <option value="1">Jan</option>
   <option value="2">Feb</option>
   :
   :
</select>

这是您可以使用 Jquery 获取值的方式

//To get the value of selected item
$("#bulan").click(function () { console.log($("#bulan").val()); });

//To get the text of selected item
$("#bulan").click(function () { console.log($( "#bulan option:selected" ).text()); }); 

如果您需要如下选择

<select id="bulan">
   <option value="Jan">Jan</option>
   <option value="Feb">Feb</option>
   :
   :
</select>

你可以的

$month_names = array(
        "Januari"=>"Januari","Feb"=>"Feb", .....
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-04
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多