use*_*505 5 html javascript javascript-events
嗨,我试图将onclick事件上的变量传递给javascript函数.我正在尝试以下方式,我无法在javascript函数中获取输入值.(我期待1的警报.)这是正确的方法吗?请帮忙.
<head>
<script>
function submit_value(form) {
alert(form.ip.value);
}
</script>
</head>
<table>
<tr>
<form>
<td>
<input id="ip" type="text" value="1">
</td>
<td>
<a href="javascript:;" onClick="submit_value(this)">Button</a>
</td>
</form>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
你的脚本不知道form是什么。您需要指定document.forms[0].ip.value。
如果文档中有多个表单,那么最好先将表单元素存储在变量中。你可以在表格上有一个ID...
<form id="formID">
在submit_value函数中你可以有
var myForm = document.getElementById('formID');
alert(myForm.ip.value);
编辑:
您可以用于this.form锚标记的onClick。