简体   繁体   English

输入两个输入字段值时,javascript函数不起作用

[英]javascript function does not work when two input field values are entered

When I run this and click submit, it only functions if I do not enter both values into the form. 当我运行此命令并单击“提交”时,仅当我没有在表单中输入两个值时,它才起作用。 If I enter just one field or no data, then it returns as expected but when I enter both town and email, the userText flashes on the screen then disappears. 如果我仅输入一个字段或不输入任何数据,则它将按预期方式返回,但是当我同时输入城镇和电子邮件时,userText在屏幕上闪烁然后消失。 What am I missing?? 我想念什么? Thanks for any help.... 谢谢你的帮助....

    <html>

<form>
  <fieldset>
    <label>What's Your Home Town?
        <input type="text" id="town" size="40" maxlength="60" required>
    </label>    
    <label>Email Address?
        <input type="email" id="email" size="40" maxlength="60" required>
        </label>
    <label>Submit
    </label>
        <input type="submit" value="Submit" onclick="validateForm();">
  </fieldset>
        <p id = "text"></p>

</form>

<script>



        function validateForm() {
            var userTown = document.getElementById('town').value;
            var userEmail = document.getElementById('email').value;
            var userText = userTown +'\'s a Great Town!' + ' click this link to email someone who cares ' + userEmail;
            //alert(userText + ' Follow This Link to Send an Email to Someone who Cares ' + userEmail);
           document.getElementById('text').innerHTML=userText;    

        }

        </script>
</html>

Since you have a submit type button in the form so form is submitted. 由于您在表单中有一个submit类型按钮,因此可以提交表单。 Changing it to button type will solve your problem 将其更改为button类型将解决您的问题

 function validateForm() { var userTown = document.getElementById('town').value; var userEmail = document.getElementById('email').value; var userText = userTown + '\\'sa Great Town!' + ' click this link to email someone who cares <a href="mailto' + userEmail +'">' + userEmail +'</a>'; //alert(userText + ' Follow This Link to Send an Email to Someone who Cares ' + userEmail); document.getElementById('text').innerHTML = userText; } 
 <form> <fieldset> <label>What's Your Home Town? <input type="text" id="town" size="40" maxlength="60" required> </label> <label>Email Address? <input type="email" id="email" size="40" maxlength="60" required> </label> <label>Submit </label> <input type="button" value="Submit" onclick="validateForm();"> </fieldset> <p id="text"></p> </form> 

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

相关问题 onkeyup javascript函数在输入字段上无法正常工作 - onkeyup javascript function does not work properly on input field 在javascript中计算两个输入字段值 - calculate two input field values in javascript 为什么用 Javascript 填充输入字段不起作用 - Why does populating the input field with Javascript not work 在javascript的输入数字类型字段中输入值时如何显示输入文本字段? - How to show a input text field when a value is entered in input number type field in javascript? 两个输入值的加法和乘法不起作用 - Addition and multiplication of two input values does not work 在javascript中编辑html输入字段值不会触发on。(&#39;change&#39;,…)函数 - Editing html input field values in javascript does not trigger on.('change', …) function 在另一个输入字段中输入文本时清除输入字段 - clear input field when text is entered into another input field 输入到输入字段时,angular-ui日期选择器不允许格式dd.MM.yyyy - angular-ui datepicker does not allow format dd.MM.yyyy when entered into the input field 使用javascript验证文本字段中的输入(需要为两个值之一) - Validate input in a text field with javascript (needs to be one of two values) 两个空输入字段检查问题使用 JavaScript function - Two empty input field checking problem using JavaScript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM