[英]How to get all the selected options in a drop down list with JavaScript?
I have a drop down list, and user can select multi options. 我有一个下拉列表,用户可以选择多个选项。
So this is part of my code in firtfile.inc 所以这是我在firtfile.inc中的代码的一部分
<div class="col-md-8">
<!-- Change report heading so we can grab it on the report page -->
<select name="SearchIn[]" multiple="multiple" onchange="javascript:this.form.Heading.value=this.options[this.selectedIndex].text;" required="required" title="Section">
<option value="" disabled selected>Select</option>
<?php
//Loop over the options
for($i=0;$i<sizeof($SearchCriteria);$i++){
?>
<option value="<?php echo $SearchCriteria[$i]['value'];?>"<?php if($SearchCriteria[$i]['value']=='FacultyName'){echo ' selected="selected"';}?>>
<?php echo $SearchCriteria[$i]['display'];?>
</option>
<?php
}//!End of looping over search criteria
?>
</select>
<!-- Hidden heading field for search report -->
<input type="hidden" name="Heading" valtue="" />
</div>
in another php file, I have included firstfile.inc, and i want to get the list of options that user has selected from drop down list. 在另一个php文件中,我包括firstfile.inc,我想获取用户从下拉列表中选择的选项列表。 so I have the folloing in the php file to get the selected options. 因此,我在php文件中添加了以下选项。
$Heading = $dat->if_default('Heading', '');
but it only gives me the first option that the user has selected, but I need all of them. 但这只是给我用户选择的第一个选项,但我需要所有这些。 How can I get them? 我怎样才能得到它们?
First of all, I highly recommend that you use jQuery , it will make any DOM manipulation a lot easier. 首先,我强烈建议您使用jQuery ,它将使任何DOM操作都更加容易。
It's not clear what output you expect, but supposing you're expecting a comma separated string like: 目前尚不清楚您期望的输出是什么,但是假设您期望使用逗号分隔的字符串,例如:
Criteria1, Criteria2, Criteria3
You could: 你可以:
1) Just use PHP to give you that, with something like: 1)只需使用PHP即可,例如:
$SearchIn = $_POST["SearchIn"];
$selectedArr = array();
foreach($SearchCriteria as $item) {
if (in_array($item["value"], $SearchIn)) {
$selectedArr[] = $item["display"];
}
}
$criteria = implode(", ", $selectedArr); // will contain: Criteria1, Criteria2, Criteria3
2) If you want to do it on the client and store the values in <input id="heading"/>
, you can use jQuery and do this (your HTML would change a little, to include the IDs): 2)如果要在客户端上执行此操作并将值存储在<input id="heading"/>
,则可以使用jQuery并执行此操作(您的HTML会稍作更改,以包含ID):
<form action="index.php" method="POST">
<div class="col-md-8">
<!-- Change report heading so we can grab it on the report page -->
<select id="section" name="SearchIn[]" multiple="multiple" required="required" title="Section">
<option value="" disabled selected>Select</option>
<?php for($i = 0; $i < sizeof($SearchCriteria); $i++) { ?>
<option value="<?php echo $SearchCriteria[$i]['value'];?>"<?php if($SearchCriteria[$i]['value']=='FacultyName'){echo ' selected="selected"';}?>>
<?php echo $SearchCriteria[$i]['display'];?>
</option>
<?php } ?>
</select>
<!-- Hidden heading field for search report -->
<input id="heading" type="hidden" name="Heading" value="" />
</div>
<input type="submit">
</form>
<script>
$('#section').on("change", function() {
selectedArray = new Array();
$(this).find("option:selected:not([disabled])").each(function() {
selectedArray.push(this.text);
});
$("#heading").val(selectedArray.join(", "));
});
</script>
3) For a pure JavaScript solution (use the same HTML as above): 3)对于纯JavaScript解决方案(使用与上述相同的HTML):
<script>
document.getElementById("section").onchange=function(){
selectedArray = new Array();
for (x = 0; x < this.length; x++) {
if (this[x].selected && !this[x].disabled) {
selectedArray.push(this[x].text);
}
}
document.getElementById("heading").value = selectedArray.join(", ");
};
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.