2017-02-20 7 views
-1

Так что мой учитель дал нам задание. Частью этого является то, что если флажок установлен, когда я нажимаю кнопку, он скажет, зарегистрирован ли «клиент» для информационного бюллетеня или нет. Она не научила нас jQuery, поэтому я не думаю, что она хочет, чтобы мы ее использовали. Есть ли способ сделать это без jQuery?Как проверить, установлен ли этот флажок или нет без jquery

Это код моего учителя в теле:

<div class="error"></div> 

<label for="customerFirst">First Name:</label> 
<input type="text" id="customerFirst"> 

<label for="customerLast">Last Name:</label> 
<input type="text" id="customerLast"> 

<label for="customerEmail">Email:</label> 
<input type="text" id="customerEmail"> 

<label for="customerNewsletter" style="font-weight: normal;">Check here to subscribe to the newsletter.</label> 
<input type="checkbox" id="customerNewsletter" checked> 

<button id="submit" onclick=Create()>Create Customer</button> 
<button id="reset" onclick=Reset()>Reset Form</button> 

В сценарии, я написал:

 var customer = { 
      first: "", 
      last: "", 
      email: "",}; 

     var Replace = function() { 
      customer.first = document.getElementById('customerFirst').value; 
      customer.last = document.getElementById('customerLast').value; 
      customer.email = document.getElementById('customerEmail').value; 
     } 

     var Create = function() { 
      Replace(); 
      document.getElementById('customerList').innerHTML +=(customer.first + " " + customer.last + '\'s email is ' + customer.email +); 
      document.getElementById('customerFirst').value = ""; 
      document.getElementById('customerLast').value = ""; 
      document.getElementById('customerEmail').value = ""; 
     } 

     var Reset = function() { 
      document.getElementById('customerFirst').value = ""; 
      document.getElementById('customerLast').value = ""; 
      document.getElementById('customerEmail').value = ""; 
      document.getElementById('customerList').innerHTML = ""; 
     } 

Зная, как проверить флажок будет получить мне гораздо ближе к завершению мой и заставить меня казаться немного менее некомпетентным для моего учителя. Спасибо за любую помощь.

ответ

0

Вы можете использовать атрибут checked, чтобы увидеть, если элемент проверяется или нет

document.getElementById({id}).checked