【问题标题】:Input type image doesn't submit the form when you hit enter当您按 Enter 时,输入类型图像不提交表单
【发布时间】:2013-02-06 14:27:33
【问题描述】:

我有一个表单,您需要单击以提交表单的输入为 type="image"。我想这样做,当您专注于表单的另一个输入并点击输入表单时(就像您单击输入类型图像一样)。

我需要使用 javascript 来创建这种行为吗?

这是默认行为并被认为具有良好的可访问性、可用性和用户体验吗?我很确定我在很多网站上都看到过。

【问题讨论】:

  • 提交按钮应该是 type="submit",所以如果可以的话,请更改它。如果没有,则需要在文本输入中添加按键监听,当输入=回车时,触发提交“图片”
  • 所以这个按键监听器是javascript?

标签: html forms


【解决方案1】:

您的表单需要一个action 属性,指将处理结果的脚本 - 例如:

<form id="myform" action="./myform.php">
    <!-- the rest of your form -->
</form>

如果没有action 属性,表单将无法提交,因为浏览器不知道将其提交到哪里。

【讨论】:

    【解决方案2】:

    你用过&lt;form&gt; &lt;/form&gt;标签吗 试试这个>更新的代码

    <script type="text/javascript">
        function submitform_myfn()
        {
          alert("form getting submitted");
          document.forms["myform"].submit();
          if(document.getElementById('a').value!="")
            {
                alert("input box value "+document.getElementById('a').value);
            }
            else
            {
                alert("Please enter some value in input box and submit again");
            }
        }
    
    
    
        </script>
    
    
    
    <form name="myform">
    Put some value for input box and try submitting form by clicking image
    <input type="text" id="a" name="a" />
    
    <input type="image"   style=" background:url(http://www.youthcancertrust.org/cms/site/images/RTEmagicC_play_button.png.png) no-repeat; display:block; width:300px; height:306px; "  onclick="submitform_myfn()" />
    
    
    </form>
    

    【讨论】:

    • 类型必须是图片而不是提交。
    • 我已将类型从提交按钮更改为图像并更新了上面的代码。试一试
    • 他的问题不是点击input type=image不提交表单,而是回车不提交表单。
    • 好吧,jdln 说“我想这样做,所以当您专注于表单的另一个输入并点击输入表单时(就像您单击输入类型图像一样)。”。如果您在
      标签中有输入元素,并且如果您将光标保持在任何输入框上,然后按 Enter,它将被提交。所以我觉得这就是他所期待的……!!?请尝试上面的代码
    猜你喜欢
    • 2011-12-17
    • 2015-05-21
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2010-10-24
    • 2022-12-13
    相关资源
    最近更新 更多