简体   繁体   English

onClick在表单内部不起作用?

[英]onClick didn't work inside form?

Usually I see people use form to create input : 通常我看到人们使用表格来创建输入:

<form action="">
<table>
    <tr>
        <td><label for="Fname">First Name</label></td>
        <td><input type="text" name="Fname" class="form-control" id="fname"></td>
    </tr>
    <tr>
        <td><label for="Lname">Last Name</label></td>
        <td><input type="text" name="Lname" class="form-control" id="lname"></td>
    </tr>
    <tr>
        <td><label for="address">address</label></td>
        <td><input type="text" name="address" class="form-control" id="address"></td>
    </tr>
    <tr>
        <td><label for="phone">phone</label></td>
        <td><input type="text" name="phone" class="form-control" id="phone"></td>
    </tr>
    <tr>
        <td><input type="submit" value="submit" onclick="submit();"></td>
    </tr>
</table>
</form>

It's make my submit button didn't work, but as soon as I remove the form tag it's work : 这使我的提交按钮不起作用,但是一旦我删除了表单标签,它就起作用了:

<table>
    <tr>
        <td><label for="Fname">First Name</label></td>
        <td><input type="text" name="Fname" class="form-control" id="fname"></td>
    </tr>
    <tr>
        <td><label for="Lname">Last Name</label></td>
        <td><input type="text" name="Lname" class="form-control" id="lname"></td>
    </tr>
    <tr>
        <td><label for="address">address</label></td>
        <td><input type="text" name="address" class="form-control" id="address"></td>
    </tr>
    <tr>
        <td><label for="phone">phone</label></td>
        <td><input type="text" name="phone" class="form-control" id="phone"></td>
    </tr>
    <tr>
        <td><input type="submit" value="submit" onclick="submit();"></td>
    </tr>
</table>

My js only like this : 我的js只有这样:

var Fname= document.getElementById("fname");
var Lname= document.getElementById("lname");
var Address= document.getElementById("address");
var Phone= document.getElementById("phone");
var Result= document.getElementById("result");


function submit(){
    var valueFname=Fname.value;
    var valueLname=Lname.value;
    var valueAddress=Address.value;
    var valuePhone=Phone.value;
    Result.innerHTML="hello, "+valueFname+" "+valueLname+" good to see you. Your contact number is "+valuePhone+" , and your address "+valueAddress;
}

Why cant I wrap all my input inside form? 为什么我不能将所有输入都包装在表单中? does it need additional code? 需要其他代码吗?

its make my submit button didnt work 它使我的提交按钮不起作用

No, your submit button is working fine. 不,您的提交按钮可以正常工作。 Therein lies the problem you're seeing, in fact. 实际上,这就是您所看到的问题。 Your JavaScript code is executing, but the time between showing any output and reloading the page because you've submitted a form is nearly instantaneous. 您的JavaScript代码正在执行,但是由于您提交了表单,因此在显示任何输出到重新加载页面之间的时间几乎是瞬时的。

So your JavaScript works. 因此您的JavaScript可以正常工作。 And your form works. 和您的形式工作。 Everything works. 一切正常。

but as soon as i remove the form tag its work 但是,一旦我删除了表单标签,它的工作便开始了

Not quite. 不完全的。 When you remove the form tag the form submission no longer works . 删除form标记后,表单提交将不再起作用 All you've done is prevent the form from submitting, because you no longer have a form. 您要做的就是阻止表单提交,因为您不再拥有表单。

why cant i wrap all my input inside form? 为什么我不能将所有输入都包装在表格中?

You can. 您可以。 But it really depends on what you're trying to do. 但这确实取决于您要执行的操作。 If you have a form and you want to submit it, use a form element. 如果您有表单并且要提交,请使用form元素。 If you don't want to submit anything as a form, you can leave it out. 如果您不想以表单形式提交任何内容,则可以将其省略。 That's its only purpose. 那是唯一的目的。

You can have inputs on the page anywhere you like, they don't need to be in forms. 您可以在页面上的任何位置输入内容,而不必使用表单。 If you just want to interact with them via JavaScript then you can do that without a form element. 如果您只想通过JavaScript与他们进行交互,则可以在不使用form元素的情况下进行操作。

Forms are made to make HTTP methods (such as POST or PUT). 制作表格以制作HTTP方法(例如POST或PUT)。 So, if you need that input's information take part in a http method, you NEED TO use form. 因此,如果您需要输入信息使用http方法,则需要使用表格。 If your Submit button is made just for internal stuff, don't use form. 如果您的“提交”按钮仅用于内部内容,请不要使用表格。

A little more information: enter link description here 更多信息: 在此处输入链接描述

Form will send the data to the file specified in action and this event is triggered by <input type="submit"/> inside a form. 表单会将数据发送到操作中指定的文件,此事件由表单内的<input type="submit"/>触发。

You can use a <button> tag inside form to achieve what you want. 您可以在表单内部使用<button>标记来实现所需的功能。

You need to set the action for your form, it is empty. 您需要为表单设置操作,该操作为空。
Or you can use jQuery 或者您可以使用jQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM