单击按钮时更新文本框的值 [英] Update textbox value when a button is clicked

查看:63
本文介绍了单击按钮时更新文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,当我点击提交按钮时,文本框不显示任何值
是否有任何错误,我只是一个新手
非常感谢您

Here is my problem, when i click the submit button, the textbox doesn't show any value Is there any mistakes, i am just a newbie Thank you very much

    <form id="form1" name="form1" method="post" >

      <p>
        <input type="submit" name="setValue" id="setValue" value="submit" onclick="setValue()"/>
      </p>
      <p>
        <label>
          <input type="text" name="bbb" id="bbb" />
        </label>
       </p>
    </form>

    <script type="text/javascript">
    function setValue()
    {
        document.getElementById('bbb').value="new value here";
    }
      </script>


推荐答案

第一个问题是为元素使用与相同的名称,所以 window.setValue 不是函数,而是提交按钮,这是一个错误。

The first issue is that you're using the same name for the element as the function, so window.setValue is not the function, but the submit button, and that's an error.

第二个问题是当您点击 submit 按钮时,表单被提交并且页面重新加载,这就是为什么您不会看到一个值,你必须阻止表单提交。

The second issue is that when you hit the submit button, the form is submitted and the page reloads, that's why you wont see a value, you have to prevent the form from submitting.

你可以用javascript来做,但最简单的方法是使用常规按钮而不是提交按钮。

You could do it with javascript, but the easiest would be to just use a regular button instead of the submit button.

<form id="form1" name="form1" method="post">
    <p>
        <input type="button" name="set_Value" id="set_Value" value="submit" onclick="setValue()" />
    </p>
    <p>
        <label>
            <input type="text" name="bbb" id="bbb" />
        </label>
    </p>
</form>
<script type="text/javascript">
    function setValue() {
        document.getElementById('bbb').value = "new value here";
    }
</script>

FIDDLE

这篇关于单击按钮时更新文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆