如何从 JavaScript 中的循环中获取选择选项的值



我想从 JavaScript 中的每个循环中获取/获取选择选项的值

法典

<?php $user = $this->admin_model->getData('user'); ?>
<select class="form-control" name="user" id="user">
<option value="">Select User to Assign</option>
<?php foreach ($user as $row): ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->user_name</option>
<?php endforeach; ?>
</select>

试试这个。

<?php $user = $this->admin_model->getData('user'); ?>
<select class="form-control" name="user" id="user" onchange="getValue(this)">
<option value="">Select User to Assign</option>
<?php foreach ($user as $row): ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->user_name;?></option>
<?php endforeach; ?>
</select>
<script>
function getValue(sel){
var optionvalue = sel.value;
alert(optionvalue);
}
</script>

function myAlert(el){
alert(el.options[el.selectedIndex].value);
}
<select onchange="myAlert(this)" class="form-control" name="user" id="user">
<option value="">Select User to Assign</option>
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
</select>

相关内容

  • 没有找到相关文章

最新更新