php – 使用JavaScript获取表单变量
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 使用JavaScript获取表单变量,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1873字,纯文字阅读大概需要3分钟。
内容图文
我有以下形式(通过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
function alterRecord(value) {
alert(value);
}
我无法找到如何获取javascript以获取表单提交的值.
解决方法:
如果你想在提交时使用Javascript做一些事情,但没有发生正常的POST或GET请求(导致旧页面卸载并加载新页面),请确保执行以下操作:
<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 onl oad="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>
行event.preventDefault()和返回false是键.我也喜欢使用addEventListener,而不是在表单中硬编码onSubmit.我想,这只是风格/偏好的问题. (尽管addEventListener允许您拥有多个处理程序.)
内容总结
以上是互联网集市为您收集整理的php – 使用JavaScript获取表单变量全部内容,希望文章能够帮你解决php – 使用JavaScript获取表单变量所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。