Member 13279001 Ответов: 1

Вопрос относительно предупреждения


Hi everyone,

I created a login form in html. when i entered data in the form it should be display all details in alert message for that i wrote and code like this.
<pre><!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
  var id1=document.getElementById("firstname").value;
  var id2=document.getElementById("lastname").value;
  var id3=document.querySelector('input[name="gender"]:checked').value;
  var id4=document.getElementById("dob").value;
  var id5=document.getElementById("email").value;
  var id6=document.querySelector('input[name="nat"]:checked').value;
  var id7=document.getElementById("city").value;
  
    alert(id1+id2+id3+id4+id5+id6+id7);
    
  
}
</script>
</head>
<body>
<form action="/action_page.php">
<h2>Login Form</h2>
<fieldset>
<legend>Personal Information:</legend>
First name:<br/>
<input type="text" name="firstname" id="firstname"><br/><br/>

Last name:<br/>
<input type="text" name="lastname" id="lastname"><br/><br/>

Gender:<br/>
<div id="rates">
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</div>
<br/>

Birthday:
<input type="date" name="bday" id="dob">
<br/><br/>

E-mail:
<input type="email" name="email" size="40" id="email">
<br/><br/>

City:
<select name="City" id="city">
<option value="Hyderabad">Hyderabad</option>
<option value="Chennai">Chennai</option>
<option value="Bangalore">Bangalore</option>
<option value="Noida">Noida</option>
</select>
<br/><br/>

Nationality:<br>
<div id="nat">
<input type="checkbox" name="nat" value="Indian" id="ind">Indian
<input type="checkbox" name="nat" value="Other" id="oth">Other
<br/><br/>
<button onclick="myFunction()">Try it</button>
</div>
</form>
</fieldset>
</body>
</html>


but i would like do in another way when i entered firstname and lastname it should display those values only in alert message or if i entered firstname,lastname,dob those values only it should display. so which value i entered in form it should display those values in one alert message.


Что я уже пробовал:

<pre>but i would like do in another way when i entered firstname and lastname it should display those values only in alert message or if i entered firstname,lastname,dob those values only it should display. so which value i entered in form it should display those values in one alert message.

1 Ответов

Рейтинг:
2

Sanjeev Maurya

try this one

function myFunction() {
  var id1=document.getElementById("firstname").value;
  var id2=document.getElementById("lastname").value;
  var id3=document.querySelector('input[name="gender"]:checked')!=null?document.querySelector('input[name="gender"]:checked').value:'';
  var id4=document.getElementById("dob").value;
  var id5=document.getElementById("email").value;
  var id6=document.querySelector('input[name="nat"]:checked')!=null?document.querySelector('input[name="nat"]:checked').value:'';
  var id7=document.getElementById("city").value;
  
    alert(id1+id2+id3+id4+id5+id6+id7);
    
  
}
Hope Its help you.