Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我正在为我的学校项目创建一个模拟电影网站。I';我有一些与“有关的问题”;选择";表单和JavaScript代码_Javascript_Html - Fatal编程技术网

我正在为我的学校项目创建一个模拟电影网站。I';我有一些与“有关的问题”;选择";表单和JavaScript代码

我正在为我的学校项目创建一个模拟电影网站。I';我有一些与“有关的问题”;选择";表单和JavaScript代码,javascript,html,Javascript,Html,所以我现在创建一个下拉列表来选择一组日期、时间和房子。我在列表中有三个选项,即“选择日期、时间和房子”,“2019年8月20日上午11:30,第一栋”和“2019年8月22日下午4:30,第二栋”。然而,提交时每个选项的预期结果并未实现。(对于上下文,EP是电影标题的首字母缩写。) 我尝试过使用单独的if语句,而不是if-else语句,但这也没有帮助 <form onsubmit= recordEPDateTime()> <select id="EPDateTime"

所以我现在创建一个下拉列表来选择一组日期、时间和房子。我在列表中有三个选项,即“选择日期、时间和房子”,“2019年8月20日上午11:30,第一栋”和“2019年8月22日下午4:30,第二栋”。然而,提交时每个选项的预期结果并未实现。(对于上下文,EP是电影标题的首字母缩写。)

我尝试过使用单独的if语句,而不是if-else语句,但这也没有帮助

<form onsubmit= recordEPDateTime()>
    <select  id="EPDateTime" style="width: 550px;">
        <option name="choose">--Choose date, time and house--</option> 
        //Default option that tells users to choose a show
        <option name="20Aug201911:30amh1">20 Aug 2019, 11:30 am, House 1</option> 
        //Details of first show
        <option name="22Aug20194:30pmh2">22 Aug 2019, 4:30 pm, House 2</option> 
        //Details of second show
    </select>
    <input type="submit" value="Choose Seats">
</form>

<br>

<script language="javascript">
    var details="";
    function recordEPDateTime (){
        details = document.getElementById(EPDateTime);
        if(details="20Aug201911:30amh1"){
            window.open("EP20AugHouse1.html");
        }
        //Opens a web page to the house layout of first show
        else if(details="22Aug20194:30pmh2"){
            window.open("EP22AugHouse2.html");
        }
        //Opens a web page to the house layout of second show
        else{
            window.alert("Select a show first.");
        }
        //Displays a window alert that they must choose a show first.
    ;}
</script>

--选择日期、时间和房子--
//告诉用户选择节目的默认选项
2019年8月20日上午11:30,1号楼
//首场演出详情
2019年8月22日下午4:30,第2栋
//第二场演出详情

var详细信息=”; 函数recordEPDateTime(){ details=document.getElementById(EPDateTime); 如果(详细信息=“20Aug201911:30amh1”){ window.open(“EP20AugHouse1.html”); } //打开一个网页以显示第一场演出的房屋布局 否则,如果(详细信息=“2294年8月22日:30pmh2”){ window.open(“EP22AugHouse2.html”); } //打开网页以查看第二场演出的房屋布局 否则{ window.alert(“首先选择一个显示”); } //显示一个窗口警报,提示他们必须先选择一个显示。 ;}
单击submit按钮时,我希望第一个选项显示警报消息,第二个选项打开指向EP20AugHouse1.html的窗口,第三个选项打开窗口并转到EP22AugHouse2.html

但似乎无论我选择哪个选项,当我按下submit按钮时,它都会打开一个指向EP20AugHouse1.html的窗口,这是第二个选项(2019年8月20日上午11:30,House 1)的预期结果,而不是其他两个选项。
有谁能帮我告诉我代码中有什么错误吗?

您没有比较详细信息及其值,而是分配了新值。在这种情况下,if语句总是返回true。为了进行比较,请使用
==
==
如果(详细信息===“20Aug201911:30amh1”){…}

另外,
id
应该用引号括起来:
document.getElementById('EPDateTime')
或者您需要创建id为字符串的
EPDateTime
变量


但我以一种不同的方式重写了代码:

<form>
  <select  id="EPDateTime" style="width: 550px;">
    <option name="choose">--Choose date, time and house--</option> 
    //Default option that tells users to choose a show
    <option name="20Aug201911:30amh1">20 Aug 2019, 11:30 am, House 1</option> 
    //Details of first show
    <option name="22Aug20194:30pmh2">22 Aug 2019, 4:30 pm, House 2</option> 
    //Details of second show
  </select>
  <input type="submit" value="Choose Seats">
</form> 

<script type="text/javascript">
  document.querySelector('input').addEventListener('click', event => {
  const select = document.querySelector('select');

  [...select].map(el => {
    if (el.selected) {
      const name = el.getAttribute('name');

      if (name === '20Aug201911:30amh1') {
        window.open('EP20AugHouse1.html');
      } else if (name === '22Aug20194:30pmh2') {
        window.open('EP22AugHouse2.html');
      } else if (name === 'choose') {
        window.alert('Select a show first.');
      }
    }
  });
});
</script>

--选择日期、时间和房子--
//告诉用户选择节目的默认选项
2019年8月20日上午11:30,1号楼
//首场演出详情
2019年8月22日下午4:30,第2栋
//第二场演出详情
document.querySelector('input')。addEventListener('click',event=>{
const select=document.querySelector('select');
[…选择].map(el=>{
如果(选定标高){
const name=el.getAttribute('name');
如果(名称=='20Aug201911:30amh1'){
window.open('EP20AugHouse1.html');
}否则如果(名称=='2294年8月22日:30pmh2'){
window.open('EP22AugHouse2.html');
}else if(name=='choose'){
window.alert('先选择一个节目');
}
}
});
});
  • 对于HTML,必须在
    中包装注释
  • 选项没有“name”属性
  • 其余的是前面提到的@undef_user
const recordEPDateTime=()=>{
event.preventDefault();
常量s=event.target.elements['select'];
const ov=s.options[s.selectedIndex].value;
如果(!!ov){
window.alert('Will open/'+ov+'.html');
}否则{
window.alert('首先选择一个选项');
}
};

-挑选-
2019年8月20日上午11:30,1号楼
2019年8月22日下午4:30,第2栋

谢谢您的回复。我已经将代码复制到了我的网站中,但是只有当我点击按钮时页面才会重新加载。代码中是否有我需要替换的内容?是的!代码按我所希望的那样工作,我只需修改几行代码(例如,将window.alert改写为window.open的非“选择选项”选项以及选项的值。非常感谢!很高兴提供帮助:)