gpt4 book ai didi

javascript - 如何从form_dropdown中获取选定的文本?

转载 作者:行者123 更新时间:2023-11-28 07:53:29 24 4
gpt4 key购买 nike

我无法获取 form_dropdown 中的文本,它总是返回选项值。请帮忙。

查看

<?php echo form_open_multipart('upload/do_upload');?>
<?php
$cont ='';
$dept='';
foreach($level->result() as $row) {
$cont = $row->userlevel;
$dept = $row->department;
}
if(strtoupper($cont)=="USER") {
echo "<strong>".$dept."&emsp;</strong>";
}
else {
echo form_dropdown('department_name', $dept_name,'','id="department_name"');
}
?>
<br/><br/>
<div class="btn-group">
<span id="status2"><?php echo form_dropdown('document_name', $document_name, '', 'id="document_name"'); ?></span>
</div>
<br/><br/>
<label for="file">Select File To Upload:</label>
<input type="file" name="userfile" multiple class="btn btn-file"/>
</br></br>
<input type="submit" value="Upload File" class="btn btn-primary"/>
<?php echo form_close(); ?>

<script>
window.onload = function() {
var form_data = {
dept_name: "<?php echo $dept; ?>",
ajax: 1
};
$.ajax({
url:"<?php echo site_url("document/document_dept_received"); ?>",
data:form_data,
type:'POST',
success: function(msg){
document.getElementById("status2").innerHTML = msg;
}
});
};

$("#department_name").change(function() {
var form_data = {
dept_name: $("#department_name option:selected").text(),
ajax: 1
};
$.ajax({
url:"<?php echo site_url("document/document_dept_received"); ?>",
data:form_data,
type:'POST',
success: function(msg){
document.getElementById("status2").innerHTML = msg;
}
});
});
</script>

Controller

$upload_data = $this->upload->data();
$data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
$file_array = array(
'image' => $data['thumbnail_name'],
'image_name' => $upload_data['file_name'],
//'description' => "",
'date_created' => date('Y-m-d H:i:s', now()),
'date_modified' => date('Y-m-d H:i:s', now()),
'author' => $this->session->userdata('username'),
'size' => $upload_data['file_size'],
'type' => $upload_data['image_type'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
'document_name' => $this->input->post("document_name"),
'department' => $this->input->post("department_name"),
//'notes' => "",
);

当我尝试这个时...

print_r($this->input->post("document_name"));
print_r($this->input->post("department_name"));

它显示 1 和 1...

我想要的是文本而不是选项的 ID 或值。示例:我选择了IT部门和文件夹1,它将显示/记录IT部门和文件夹1到数据库。

最佳答案

检查这个:http://jsfiddle.net/Lev0sjjx/2/

注意:jquery 代码仅用于演示目的

HTML

<select id="test">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>

Javascript/jQuery

function getSelectedText(elementId) {
var elt = document.getElementById(elementId);

if (elt.selectedIndex == -1)
return null;

return elt.options[elt.selectedIndex].text;
}

$(function() {
$('#test').change(function() {
var text = getSelectedText('test');
alert(text);
});
});

关于javascript - 如何从form_dropdown中获取选定的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26462023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com