javascript - 无法将值发布到程序

标签 javascript jquery html

我正在尝试根据用户选择(通过单选按钮)将文本值发布到不同的 python 程序。程序在单一表单操作下运行良好

<form action='/cgi-bin/prog1.py' method='POST'>
    ...text input1
    ...text input2
    ...submit

</form>

但是当使用单选按钮时,文本值不会发布到程序中。

这是我尝试过的代码

<!DOCTYPE html>
<html>
 <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type="text/javascript">
  function whichsite(form){
    var sites = form.elements.site, i = sites.length;
    while (--i > -1){
    if(sites[i].checked){
        return sites[i].value;
        }
    }
  }
  </script>
 </head>
 <body>
  <form action="#" onsubmit="window.open(whichsite(this)); return false; method='POST' ">
  <b>Program Jump</b>
  <p>
  Enter PDB ID:<input type="text" name="PDB_ID"><br>
  Enter PDB Chain:<input type="text" name="Chain_ID"><br>
  <label><input type="radio" name="site" value="/cgi-bin/prog1.py">P-P</label>
  <label><input type="radio" name="site" value="/cgi-bin/prog2.py">P-L</label>
  <label><input type="radio" name="site" value="/cgi-bin/prog3.py">P-C</label>
  <p>
  <input type="submit" value="Submit">
  </form>
 </body>
</html>

帮帮我!

最佳答案

我认为你应该在onsubmit方法而不是window.open方法中更改表单的action属性值,你可以尝试一下像这样:

<!DOCTYPE html>
<html>
<head>


<title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type="text/javascript">
  function whichsite(form){
    var sites = form.elements.site, i = sites.length;
    while (--i > -1){
    if(sites[i].checked){
        // here change the action value instead of return the site
        form.action = sites[i].value;
        }
    }
  }
  </script>
 </head>
 <body>
  <form action="#" onsubmit="whichsite(this); return false;" method="POST">
  <b>Program Jump</b>
  <p>
  Enter PDB ID:<input type="text" name="PDB_ID"><br>
  Enter PDB Chain:<input type="text" name="Chain_ID"><br>
  <label><input type="radio" name="site" value="/cgi-bin/prog1.py">P-P</label>
  <label><input type="radio" name="site" value="/cgi-bin/prog2.py">P-L</label>
  <label><input type="radio" name="site" value="/cgi-bin/prog3.py">P-C</label>
  <p>
  <input type="submit" value="Submit">
  </form>
 </body>
</html>

或者,你可以尝试改变radio的change事件中action的值,希望对你有帮助~~~

关于javascript - 无法将值发布到程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39444694/

相关文章:

html - 位置 :absolute 之后的静态元素

html - 带有背景图像的粘性导航栏

javascript - 从数组返回对象时排除属性?

javascript - jQuery fadeIn 动画对 Ajax 加载有影响吗?

javascript - AngularJS - 尝试为 url slug 创建自定义指令

javascript - 列表项事件悬停效果

javascript - 自定义滚动条太快

jquery - 如何在laravel Controller 中获取多个图像文件/数据

迭代期间的jQuery addclass但仅更改当前div

javascript - onclick 函数未定义