javascript将文本输入传递给onclick处理程序

时间:2011-09-15 07:04:36

标签: javascript html onclick

让我们说我的表格如下。如何将名为“configname”的文本框中的值传递给onclick函数处理程序??

<form id="loadconfigform">
        Config Name: <input type="text" name="configname" />
        <input type="button" value="Submit" onclick="onLoadConfigPress(configname)" />
    </form>

3 个答案:

答案 0 :(得分:23)

输入id输入字段:

<input type="text" id="configname" name="configname" />

现在修改点击处理程序,如下所示:

<input type="button" value="Submit" 
  onclick="onLoadConfigPress(document.getElementById('configname').value)" />

或者,如果该页面上只有一个表单,您还可以使用forms数组:

<input type="button" value="Submit" 
  onclick="onLoadConfigPress(document.forms[0].configname.value)" />

答案 1 :(得分:1)

<form id="loadconfigform">
   Config Name: <input type="text" id="configname" name="configname" />
   <input type="button" value="Submit"
     onclick="onLoadConfigPress(document.getElementById('configname').value);" />
</form>

答案 2 :(得分:1)

<form id="loadconfigform">
        Config Name: <input type="text" name="configname" />
        <input type="button" value="Submit" onclick="onLoadConfigPress(document.getElementsByName('configname')[0].value)" />
    </form>

只需使用其名称调用它即可。我建议使用ID。

如果您有其他具有相同名称的元素,这将无法使用,因此请像使用其他答案一样使用ID。

相关问题