Html 使用 Div onClick 提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14174362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:45:25  来源:igfitidea点击:

Form Submit with Div onClick

htmlforms

提问by Max I

I'm trying to use a div to submit a form. Here's the HTML:

我正在尝试使用 div 提交表单。这是 HTML:

<form name="search-form" 
      id="search-form" 
      action="php.php" 
      class="form-search">

    <input style="color: black; background: rgba(255,255,255,0.5); padding: 15px; font-size: 18px;" 
           type="text" 
           name="search" 
           class="input-medium search-query">

        <div class="button1" onClick="document.forms["search-form"].submit();">
            <a href="#">
                <img  alt="" src="/img/buttons/icon1.png" />
            </a>
        </div>
    </form>

The form does not submit when the div is clicked. Could someone point out the problem? thanks. I've been trying this for an hour at least.

单击 div 时,表单不会提交。有人能指出问题吗?谢谢。我已经尝试了至少一个小时。

The a hrefis needed to keep the styling, but even if I remove it and left with just the icon, the submit doesn't work; nor does it work if I apply the onclickto the image.

a href需要保持造型,但即使我删除它,只剩下了图标,提交不工作; 如果我将 应用于onclick图像,它也不起作用。

回答by CoursesWeb

Add search-formbetween single quotes:

search-form在单引号之间添加:

onClick="document.forms['search-form'].submit();"

回答by ZombieSpy

You can't just use quotes in quotes. Use single quotes to fix:

您不能只在引号中使用引号。使用单引号修复:

<form name="search-form" id="search-form" action="php.php" class="form-search">
    <input style="color: black; background: rgba(255,255,255,0.5); padding: 15px; font-size: 18px;" type="text" name="search" class="input-medium search-query">
    <div class="button1" onClick="document.forms['search-form'].submit();">
        <a href="#">
            <img alt="" src="img/buttons/icon1.png" />
        </a>
    </div>
</form>

回答by Kjartan

It may not have anything to do with the error (or maybe it does, due to invalid html?), but you should add an end-tag to your inputtag, right at then end here:

它可能与错误无关(或者可能是因为无效的 html?),但是您应该在标签中添加一个结束input标签,然后在此处结束:

<input style="color: black; (... etc) " 
       type="text" 
       name="search" 
       class="input-medium search-query" />