Php 使用JavaScript获取表单变量

Php 使用JavaScript获取表单变量,php,javascript,html,Php,Javascript,Html,我有下面的表单(通过php实现),当选择单选按钮时,我希望将该值传递给javascript函数,然后我可以处理该函数 <form id=\"form1\" name=\"form1\" method=\"\" action=\"JavaScript:alterRecord()\"> <input name=\"radiobutton\" type=\"radio\" value=\"test1\" />Test 2<p> <input name=\"ra

我有下面的表单(通过php实现),当选择单选按钮时,我希望将该值传递给javascript函数,然后我可以处理该函数

<form id=\"form1\" name=\"form1\" method=\"\" action=\"JavaScript:alterRecord()\">
<input name=\"radiobutton\" type=\"radio\" value=\"test1\" />Test 2<p>
<input name=\"radiobutton\" type=\"radio\" value=\"test2\" />Test 2<p>
<input name=\"radiobutton\" type=\"radio\" value=\"test3\" />Test 3<p>
<input name=\"radiobutton\" type=\"radio\" value=\"test4\" />Test 4
<input type=\"submit\" name=\"Submit\" value=\"Submit\" />
</form>

我无法找到如何获取javascript以获取表单提交值。

从我的头顶:

<form onSubmit="alterRecord">
  <input type="radio" id="radio1"/>
  ...
</form>

function alterRecord() {
  var radio1 = document.getElementById('radio1');
  alert(radio1.checked);
}

...
函数记录(){
var radio1=document.getElementById('radio1');
警报(无线电1.已检查);
}

操作属性应指向URL。如果希望在用户提交表单时使用javascript执行某些操作,请使用onsubmit属性。例如,onsubmit=“alert(42)”

如果从onsubmit属性中调用javascript方法并将其作为参数传递“this”,则可以访问表单标记及其所有子节点,并可以随意确定各种控件的当前状态。

首先,开始使用

现在你已经做到了,做这样的事情应该很容易:

$("input[name='radiobutton']").click(function() {
   // do stuff
});
<html>
  <head>
    <style type="text/css">
      body { background-color: black; color: white; }
    </style>
    <script type="text/javascript">
      function handleClick(event)
      {
        if (console) console.info("handling click");  
        // for Firebug debugging, if you want it

    /* do something here */
        alert(this.textBox1.value);
        // I don't like alerts but it works everywhere

        if (console) console.info("handled click");  

        event.preventDefault(); // disable normal form submit behavior
        return false; // prevent further bubbling of event
      }
      function doit()
      {
        if (console) console.info("starting doit()");
        document.forms.myform.addEventListener("submit", handleClick, true);
        return true;
      }
    </script>
  </head>
  <body onload="doit()">
    <form name="myform">
      <div>
          <textarea name="textBox1" rows="10" cols="80">
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe;
All mimsy were the borogoves,
And the mome raths outgrabe.
         </textarea>
      </div>
      <input type="submit" value="Update"/>
    </form>
  </body>
</html>

您希望javascript在单击单选按钮时启动吗?或者什么时候提交表格

如前所述,您可以使用onsubmit=“alterRecord(this);” 然后您可以使用命令,例如

alert(this.radio1.checked);
如果您将id添加到所有单选按钮中

每次单击单选按钮时,javascript都会启动

<input type="radio" onclick="alterRecord(this);" ... />


然后,“this”命令将允许您访问单击的单选按钮。

如果您想在提交时使用Javascript执行某些操作,但没有执行正常的POST或GET请求(这将导致卸载旧页面并加载新页面),请确保执行以下操作:

$("input[name='radiobutton']").click(function() {
   // do stuff
});
<html>
  <head>
    <style type="text/css">
      body { background-color: black; color: white; }
    </style>
    <script type="text/javascript">
      function handleClick(event)
      {
        if (console) console.info("handling click");  
        // for Firebug debugging, if you want it

    /* do something here */
        alert(this.textBox1.value);
        // I don't like alerts but it works everywhere

        if (console) console.info("handled click");  

        event.preventDefault(); // disable normal form submit behavior
        return false; // prevent further bubbling of event
      }
      function doit()
      {
        if (console) console.info("starting doit()");
        document.forms.myform.addEventListener("submit", handleClick, true);
        return true;
      }
    </script>
  </head>
  <body onload="doit()">
    <form name="myform">
      <div>
          <textarea name="textBox1" rows="10" cols="80">
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe;
All mimsy were the borogoves,
And the mome raths outgrabe.
         </textarea>
      </div>
      <input type="submit" value="Update"/>
    </form>
  </body>
</html>

正文{背景色:黑色;颜色:白色;}
函数handleClick(事件)
{
如果(控制台)控制台信息(“处理点击”);
//用于Firebug调试,如果需要的话
/*在这里做点什么*/
警报(this.textBox1.value);
//我不喜欢警报,但它在任何地方都有效
如果(控制台)控制台信息(“点击处理”);
event.preventDefault();//禁用正常表单提交行为
return false;//防止事件进一步冒泡
}
函数doit()
{
if(console)console.info(“启动doit()”);
document.forms.myform.addEventListener(“提交”,handleClick,true);
返回true;
}
那是一件漂亮的衣服
在瓦贝河中回旋和摇摆;
所有的米姆西都是波罗戈夫人,
而这一时刻却被超越了。

event.preventDefault()
return false
是键。我也喜欢使用addEventListener,而不是在表单中硬编码onSubmit。我想这只是风格/偏好的问题。(虽然addEventListener允许您拥有多个处理程序。)

我不想使用url,我想使用javascript函数,所以不要设置action属性。如果你想用javascript处理表单,你需要的属性是onsubmit。jQuery对某些人来说很好,但它是一条完全不同的学习曲线&你可以按照OP的要求去做,而不需要使用jQuery,也不需要做任何花哨的事。这一点值得注意,但它仍然是问题的有效解决方案。我没有试图传播这将刷新整个页面,我希望避免。您可以在事件处理程序中停止事件传播。如果您使用像jQuery这样的库,它可以像在处理程序末尾添加“returnfalse”一样简单。jQuery(“#我的表单”).submit(function(){return false})我在向客户机显示HTML/CSS设计时经常使用它,即jQuery('a')。单击(function(){return false});这看起来很完美。您将如何修改它以使用onsubmit?但是我用onsubmit调用handleevent,我无法让它注册