使用php或javascript重新加载选项值
有没有办法用php或javascript在select中重新加载一些选项值? 因此,当用户更改一个“选择”的值时,其他两个选择应该从值更改 例如: 你必须选择有多少成年人要去旅行。(最多6人) 所以你选择4 当您选择了其他2个选项(适用于儿童和婴儿)后,更改为剩余的数量(此处为2) 因此,当您在“儿童”选项中选择1时,婴儿将变为1 我原以为这样做会奏效,但我错了使用php或javascript重新加载选项值,php,javascript,select,option,Php,Javascript,Select,Option,有没有办法用php或javascript在select中重新加载一些选项值? 因此,当用户更改一个“选择”的值时,其他两个选择应该从值更改 例如: 你必须选择有多少成年人要去旅行。(最多6人) 所以你选择4 当您选择了其他2个选项(适用于儿童和婴儿)后,更改为剩余的数量(此处为2) 因此,当您在“儿童”选项中选择1时,婴儿将变为1 我原以为这样做会奏效,但我错了 <select name="dpl_volwassenen" id="dpl_volwassenen" onchange="ge
<select name="dpl_volwassenen" id="dpl_volwassenen" onchange="getPersonen(this)">
<script type="text/javascript">
for (i=0; i<=6; i++){
myOption=new Option(i, i);
document.getElementById('dpl_volwassenen').options[i]=myOption;
}
var aantal = 6;
function getPersonen($int_aantal){
aantal = aantal - $int_aantal.value;
for (i=0; i<=aantal; i++){
myOption=new Option(i, i);
document.getElementById($int_aantal.id).dpl_volwassenen.options[i]=myOption;
}
}
</script>
</select></td>
</tr>
<tr>
<td>
<select name="dpl_kinderen" id="dpl_kinderen" onchange="getPersonen(this)">
</select>
</td>
</tr>
<tr>
<td>
<select name="dpl_babys" id="dpl_babys" onchange="getPersonen(this)">
</select>
</td>
</tr>
对于(i=0;i什么是$int\u aantal
?PHP?您的浏览器不可能知道这一点。
应该可以帮助您。您想要这样的东西吗
<script type="text/javascript">
window.onload = function () {
// Assign events in window.onload
// Get references to all the <select>s
volwassenenSelect = document.getElementById('dpl_volwassenen');
kinderenSelect = document.getElementById('dpl_kinderen');
babysSelect = document.getElementById('dpl_babys');
volwassenenSelect.onchange = function () {
var kOldSelected, bOldSelected, i, newOption;
// Get the values of the other two selects
kOldSelected = kinderenSelect.selectedIndex;
bOldSelected = babysSelect.selectedIndex;
// When volwassenen is changed, you need to update the other lists
if (kinderenSelect.options.length > 0) {
// kinderenSelect already has some options
if (kinderenSelect.options.length < volwassenenSelect.selectedIndex + 1) {
// we are adding more options
for (i = kinderenSelect.options.length; i <= volwassenenSelect.selectedIndex; i++) {
kinderenSelect.options[i] = new Option(i, i);
}
} else {
// we are removing some options
if (kOldSelected > volwassenenSelect.selectedIndex) {
// if the selected option was greater than the number of new options, set it to the last option
kinderenSelect.selectedIndex = volwassenenSelect.selectedIndex;
}
for (i = kinderenSelect.options.length - 1; i > volwassenenSelect.selectedIndex; i--) {
kinderenSelect.removeChild(kinderenSelect.options[i]);
}
}
} else {
// kinderenSelect has no options yet, add volwassenenSelect.selectedIndex options
for (i = 0; i <= volwassenenSelect.selectedIndex; i++) {
kinderenSelect.options[i] = new Option(i, i);
}
}
if (babysSelect.options.length > 0) {
// babysSelect already has some options
if (babysSelect.options.length < volwassenenSelect.selectedIndex + 1) {
// we are adding more options
for (i = babysSelect.options.length; i <= volwassenenSelect.selectedIndex; i++) {
babysSelect.options[i] = new Option(i, i);
}
} else {
// we are removing some options
if (bOldSelected + kinderenSelect.selectedIndex > volwassenenSelect.selectedIndex) {
// make sure the total of kinderenSelect + babysSelect does not excede volwassenenSelect
babysSelect.selectedIndex = volwassenenSelect.selectedIndex - kinderenSelect.selectedIndex;
} else if (bOldSelected > volwassenenSelect.selectedIndex) {
// if the selected option was greater than the number of new options, set it to the last option
babysSelect.selectedIndex = volwassenenSelect.selectedIndex;
}
for (i = babysSelect.options.length - 1; i > volwassenenSelect.selectedIndex; i--) {
babysSelect.removeChild(babysSelect.options[i]);
}
}
} else {
// babysSelect has no options yet, add volwassenenSelect.selectedIndex options
for (i = 0; i <= volwassenenSelect.selectedIndex; i++) {
babysSelect.options[i] = new Option(i, i);
}
}
};
// These two functions make sure the total of kinderenSelect + babysSelect does not excede volwassenenSelect
kinderenSelect.onchange = function () {
if (babysSelect.selectedIndex > volwassenenSelect.selectedIndex - kinderenSelect.selectedIndex) {
babysSelect.selectedIndex = volwassenenSelect.selectedIndex - kinderenSelect.selectedIndex;
}
};
babysSelect.onchange = function () {
if (kinderenSelect.selectedIndex > volwassenenSelect.selectedIndex - babysSelect.selectedIndex) {
kinderenSelect.selectedIndex = volwassenenSelect.selectedIndex - babysSelect.selectedIndex;
}
};
};
</script>
<table>
<tr>
<td>Volwassenen</td>
<td>
<select id="dpl_volwassenen" name="dpl_volwassenen">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
</tr>
<tr>
<td>Kinderen</td>
<td>
<select id="dpl_kinderen" name="dpl_kinderen"></select>
</td>
</tr>
<tr>
<td>Babys</td>
<td>
<select id="dpl_babys" name="dpl_babys"></select>
</td>
</tr>
</table>
window.onload=函数(){
//在window.onload中分配事件
//获取所有s的引用
volwassenenSelect=document.getElementById('dpl_volwassenen');
kindenselect=document.getElementById('dpl_kinderen');
babyselect=document.getElementById('dpl_babys');
volwassenneselect.onchange=函数(){
var kOldSelected、bOldSelected、i、newOption;
//获取其他两个选择的值
kOldSelected=kindenselect.selectedIndex;
bOldSelected=BabySelected.selectedIndex;
//更改volwassenen后,需要更新其他列表
如果(kindenselect.options.length>0){
//Kindenselect已经有了一些选择
if(kindensenselect.options.lengthvolwassenneselect.selectedIndex;i--){
kindenselect.removeChild(kindenselect.options[i]);
}
}
}否则{
//kinderenSelect还没有选项,请添加volwassenenSelect.selectedIndex选项
对于(i=0;i=0){
//BabySelect已经有了一些选项
if(babyselect.options.lengthvolwassenenSelect.selectedIndex){
//如果所选选项大于新选项数,请将其设置为最后一个选项
babyselect.selectedIndex=volwassenneselect.selectedIndex;
}
对于(i=babysSelect.options.length-1;i>volwassenneselect.selectedIndex;i--){
babyselect.removeChild(babyselect.options[i]);
}
}
}否则{
//BabySelect尚未有任何选项,请添加VolwassenneSelect.selectedIndex选项
对于(i=0;i volwassenneselect.selectedIndex-kindensenselect.selectedIndex){
babyselect.selectedIndex=volwassenneselect.selectedIndex-kindensenselect.selectedIndex;
}
};
babyselect.onchange=函数(){
如果(KinderSenseSelect.selectedIndex>VolwassenSenseSelect.selectedIndex-BabySelect.selectedIndex){
KinderSenselect.selectedIndex=Volwassensenselect.selectedIndex-BabySelect.selectedIndex;
}
};
};
沃瓦森
0
1.
2.
3.
4.
5.
6.
幼稚的
婴儿
这里是另一种使用方法
<script language='javascript'>
var kinderen = new Array();
var babys = new Array();
var max_aantal = 6;
function getPersonen(sParentBox, sChildBox){
//get the value of the selected option
var sParentValue = document.getElementById(sParentBox).value;
max_aantal =max_aantal - sParentValue;
alert(max_aantal);
//load the appropriate event list to the option box
var theSel = document.getElementById(sChildBox);
//clear previous options
theSel.options.length = null;
//handle if there is no value
if (sParentValue == ''){alert("exiting"); return;}
if (sParentBox == "dpl_volwassenen") {
for(i=0; i<=max_aantal;i++){
kinderen.push(i);
}
oTargetArray = kinderen;
}else if(sParentBox == "dpl_kinderen"){
for(i=0; i<=max_aantal;i++){
babys.push(i);
}
oTargetArray = babys;
}
//split the array values by the separator
// var arrList = oTargetArray[sParentValue].split(",");
alert(oTargetArray.length);
//loop thru the array of options to add them to the dropdown
for (x=0; x < oTargetArray.length; x++)
{
var newOpt = new Option(oTargetArray[x], oTargetArray[x]);//(theText,theValue);
var selLength = theSel.length;
theSel.options[selLength] = newOpt;
}//next
}//end func
</script>
var kinderen=新数组();
var babys=新数组();
var max_aantal=6;
函数getPersonen(sParentBox、sChildBox){
//获取所选选项的值
var sParentValue=document.getElementById(sParentBox.value);
max_aantal=max_aantal-sParentValue;
警报(最大值);
//将适当的事件列表加载到选项框中
var theSel=document.getElementById(sChildBox);
//清除以前的选项
theSel.options.length=null;
//如果没有值,则处理
if(sParentValue=''){alert(“exiting”);return;}
如果(sParentBox==“dpl_volwassenen”){
对于(i=0;你想在谷歌上搜索的短语是“条件下拉javascript”,有一百万种方法可以做到这一点;-)事实上,JS变量允许使用$
。我建议避免使用它,但这是允许的。这更像是在添加元素时,onchange函数再次启动。因此,在第一次通过循环后,他再次启动该函数。是的,这就是我要寻找的。