简体   繁体   English

Javascript初学者:为什么我的表单不提交

[英]Javascript Beginner: Why isn't my form submitting

Trying to teach myself javascript here so do bear with me. 尝试在这里教自己使用javascript,所以请多多包涵。

Why isn't the following snippet working? 为什么以下代码段不起作用?

 <script type="text/javascript">
     function form() {
         var role_id = document.contextform.role.value;
         document.contextform.action="profile/" + role_id;
         document.contextform.submit();
     }
 </script>
 <form name="contextform" method="post" action="" />
     <div class="input_group">
         <select name="role" style="opacity: 0;" onchange="form();">
         <option value="none">Select Context</option>
         <option value="4">Profile 4</option>
         </select>
     </div>
 </form>

I'm basically trying to redirect the user based on the value of the option on the drop down list. 我基本上是根据下拉列表上选项的值来尝试重定向用户。 Thanks. 谢谢。

you closed the form tag when you declare it 您在声明表单时关闭了表单标签

<form name="contextform" method="post" action="" />
.
.
</form>

changed it to this and try again 改成这个然后再试一次

<form name="contextform" method="post" action="">
.
.
</form>

Your HTML is invalid 您的HTML无效

 <form name="contextform" method="post" action="" />

should be 应该

 <form name="contextform" method="post" action="">

Also, your function name form() is a reserved word. 另外,您的函数名称form()是保留字。 Try something else for the name of that function. 尝试使用其他名称作为该函数的名称。

请将函数名称form()更改为myForm()其他名称。

Frist of all, 所有人的拳头

<select name="role" style="opacity: 0;" onchange="form();">

the opacity there makes it disappeared. 那里的不透明性使其消失了。 So, you can just remove the style. 因此,您可以删除样式。

Secondly, the name form seemsed to be a reserved keyword. 其次,名称form似乎是保留关键字。 So, you can't use that name. 因此,您不能使用该名称。 Try to put some other name like form_handler() 尝试输入其他名称,例如form_handler()

<script type="text/javascript">
     function form_handler() {
         var role_id = document.contextform.role.value;
         document.contextform.action="profile/" + role_id;
         document.contextform.submit();
     }
 </script>

and change your html as well (with the style removed) 并更改您的html(删除样式)

<select name="role" onchange="form_handler();">

If you change the name of your function to postForm in both the function declaration and in the onchange handler, it seems to work for me. 如果在函数声明和onchange处理程序中都将函数名称更改为postForm ,这似乎对我postForm There must be a conflict with a global identifier or reserved word named "form". 必须与名为“ form”的全局标识符或保留字冲突。

That script really isn't needed, try: 确实不需要该脚本,请尝试:

<form name="contextform" method="post" action="/profile">
 <div class="input_group">
     <select name="role" style="opacity: 0;" onchange="document.contextform.submit();">
     <option value="none">Select Context</option>
     <option value="4">Profile 4</option>
     </select>
 </div>
</form>

尝试替换document.getElementsByName('contextform')上的所有document.contextform

Nai,

The function name that you have is the same as the key word. 您具有的功能名称与关键字相同。 That means the form() is the same as the tag "form" and therefore you will get an error and the submit will not work. 这意味着form()与标签“ form”相同,因此您将得到一个错误,并且提交将无法工作。 If you change the function name to form1() it will do the trick. 如果将函数名称更改为form1(),它将可以解决问题。 Another thing, role_id value is 4 so the path in action event would be "profile/4" but the file extension is not present so i do not think you will sent to the correct. 另一件事,role_id值是4,所以动作事件中的路径将是“ profile / 4”,但文件扩展名不存在,所以我认为您不会发送给正确的人。 Below is you code with the changes that i have suggested, hope they work for you. 下面是我建议的更改的代码,希望它们对您有用。

    <script type="text/javascript">      
     function form1() 
      {          
       var role_id = document.contextform.role.value;          
       document.contextform.action="profile/" + role_id + ".html";          
       document.contextform.submit();      
      }  
   </script>  
   <form name="contextform" method="post" action="">
       <div class="input_group">          
         <select name="role" style="opacity: 0;" onchange="form1()">          
           <option value="none">Select Context</option>          
           <option value="4">Profile 4</option>          
         </select>      
       </div>  
   </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM