更改a<;选择选项>;在Javascript中,但前面的选择保留,为什么?

更改a<;选择选项>;在Javascript中,但前面的选择保留,为什么?,javascript,Javascript,我正在尝试做一个简单的脚本来检查日期,如果“day Selected”==“明天”,则更改包含交付选项的下拉列表: 下拉列表: 0-何时?(已选择) 凌晨1点 下午2点 如果day=明天,则我在javascript中删除以下选项: 下拉列表: 什么时候? 下午2点(已选) 剧本: // remove all options first document.getElementById('inputheurelivraison').options.length = 0; if (parseInt(d

我正在尝试做一个简单的脚本来检查日期,如果“day Selected”==“明天”,则更改包含交付选项的下拉列表:

下拉列表:
0-何时?(已选择)
凌晨1点
下午2点

如果day=明天,则我在javascript中删除以下选项:

下拉列表:
什么时候?
下午2点(已选)

剧本:

// remove all options first
document.getElementById('inputheurelivraison').options.length = 0;
if (parseInt(datebits[2]) == parseInt(demain)){//remove am
  document.getElementById('inputheurelivraison').options[0] = new Option("WHEN?", 0, false, false);
  document.getElementById('inputheurelivraison').options[1] = new Option("PM", 2, true, true); // new Option(text, value, defaultSelected, selected)
  alert ("<? echo t(73); ?>");
}
else {//put am
  document.getElementById('inputheurelivraison').options[0] = new Option("WHEN?", 0, true, true);
  document.getElementById('inputheurelivraison').options[1] = new Option("AM", 1, false, false);
  document.getElementById('inputheurelivraison').options[2] = new Option("PM", 2, false, false);
}
//首先删除所有选项
document.getElementById('inputheurelivraison')。options.length=0;
如果(parseInt(datebits[2])==parseInt(demain)){//remove am
document.getElementById('inputheurelivraison')。选项[0]=新选项(“何时?”,0,false,false);
document.getElementById('inputheurelivraison')。选项[1]=新选项(“PM”,2,true,true);//新选项(文本,值,默认选中,选中)
警报(“”);
}
否则{//put am
document.getElementById('inputheurelivraison')。选项[0]=新选项(“何时?”,0,true,true);
document.getElementById('inputheurelivraison')。options[1]=新选项(“AM”,1,false,false);
document.getElementById('inputheurelivraison')。options[2]=新选项(“PM”,2,false,false);
}
问题是:

假设某人填写表单,然后选择“AM”作为选项,然后将日期更改为“明天”,然后运行我的脚本并从列表中删除“AM”选项,然后选择“PM”作为“已选”。当用户提交表单时,POST数据为所选的“AM”

为什么??我选择了“PM”,当我查看HTML时,它会说“PM”为“selected”,那么为什么它不提交该值呢

先谢谢你


Joe

没有必要先删除所有选项,您可以只删除不需要的选项。类似于以下内容的内容应该是合适的(注意存储对DOM元素的引用,以便只获取一次):

var doIt=(函数(){
//对已删除选项的引用
var removedOption;
返回函数{
//获取引用以选择一次
var select=document.getElementById('inputheurelivraison');
if(parseInt(datebits[2])==parseInt(demain)){
//存储对am选项的引用,然后将其删除
removedOption=select.options[1];
select.removeChild(select.options[1]);
//选择pm(现在是索引1)
select.options[1]。selected=true;
//调试?
警报(“”);
}否则{
//替换已删除的选项
select.insertBefore(removedOption,select.options[1]);
//选择第一个选项
select.options[0]。selected=true;
}
}
}());
或者,可以将不需要的选项移动到隐藏的选择元素


以上只是一个概念验证的例子,有很多方法可以剥猫皮,底线是你不必每次都删除所有选项并重新创建你想要的选项。

谢谢你的回答。但我刚刚发现了问题所在。我会解释一下,以防对其他人有帮助:

在检查“day”是否等于“明天”并更改表单后,脚本会将AJAX中的数据发布到PHP文件中。问题的根源是:我要求脚本发布下拉列表的
selectedIndex
,而不是它的
选项[selectedIndex].value
。因此,即使选择了“PM”,我的脚本也会返回“AM”作为选项,因为
selectedIndex
总是从0、1、2等开始,而我的
值是AM=1和PM=2

所以我改变了:
document.getElementById(“inputheurelivraison”)。选择索引

用于:
document.getElementById('inputheurelivraison')。选项[document.getElementById(“inputheurelivraison”)。选择的索引。值

在AJAX中发送POST值时,现在一切正常


希望没有其他人会犯此错误:)

您的脚本很难阅读-您能在脚本中添加一些换行符以更好地格式化它吗?另外,你能给我们看一下你的HTML吗?这样我们就知道javascript的作用是什么了?@Rock-当你有多行代码示例时,不要使用`字符作为代码格式化,而是选择所有代码并按下
{}
格式化按钮(或者只需将每行代码缩进四个空格-说到空格,使用空格而不是制表符)。也不要在问题中包含注释掉的代码,除非它与问题有关。我已经格式化了你的帖子,保留了你想要的换行符。@nnnnnn谢谢。。。我正在寻找该选项,但找不到它,请存储对DOM对象的引用并重新使用它们,您不必每次都获取该元素。应该注意的是,隐藏选项元素在某些浏览器中不起作用。答案已更改,因为IE 8及更低版本不玩该游戏-(不过,它们可以处理删除的选项。您知道,您可以只使用:
document.getElementById('inputheurelivraison')。如果所有选项都有value属性或属性,则使用value
var doIt = (function() {

  // Reference to removed option
  var removedOption;

  return function(s) {

    // Get reference to select once
    var select = document.getElementById('inputheurelivraison');

    if (parseInt(datebits[2]) == parseInt(demain)) {

      // Store reference to am option then remove it
      removedOption = select.options[1];
      select.removeChild(select.options[1]);

      // Make pm selected (it's now index 1)
      select.options[1].selected = true;

      // Debug?
      alert ("<? echo t(73); ?>");

    } else {
      // Replace removed option
      select.insertBefore(removedOption, select.options[1]);

      // Make first option selected
      select.options[0].selected = true;
    }

  }
}());