要让它显示所有用户输入/选择,我需要做什么?

时间:2015-11-20 01:46:55

标签: javascript jquery html

我需要在javascript / jquery警告框中显示以下表单中的用户输入,每条信息之间都有换行符。我已经尝试过实现这个jquery警报,但我并没有比以前更好。这就是我到目前为止所做的。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Questions n Shiz</title>
    <link rel="stylesheet" href="css/normanlize-signup.css">
    <link href='http://fonts.googleapis.com/css?family=Nunito:400,300' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/signup.css">
    <link rel="icon" href="favicon.ico">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>

            $('#formid').submit(function (e) {
                e.preventDefault();
                alert(JSON.stringify($('#formid')));
              });

    </script>
</head>
<body>

  <form>

    <h1>Enter Thou'st Information</h1>

    <fieldset>

      <legend><span class="number">1</span> Your basic info</legend>

    <label for="name">Name:</label>
    <input type="text" id="name" name="user_name" placeholder="Rob Neale">

    <label for="mail">Email:</label>
    <input type="email" id="mail" name="email" placeholder="robneale@gmail.com">

    <label for="password">Password:</label>
    <input type="password" id="password" name="password" placeholder="robrastamanneale">

    <label>Gender/Sex:</label>
    <input type="radio" id="male" value="male" name="user_gender"><label for="under_16" class="light">Male</label>
      <br>
    <input type="radio" id="female" value="female" name="user_gender"><label for="over_16" class="light">Female</label>

    </fieldset>

    <fieldset>

      <legend><span class="number">2</span> Your profile</legend>

      <label for="bio">Biography:</label>
    <textarea id="bio" name="user_bio" placeholder="Some words go in here ;)"></textarea>

      <label for="house">House:</label>
      <select id="house" name="user_job">
        <optgroup label="House">
          <option value="bradman">Bradman</option>
          <option value="chisholm">Chisholm</option>
          <option value="helpmann">Helpmann</option>
          <option value="mawson">Mawson</option>
          <option value="mccubbin">McCubbin</option>
          <option value="oliphant">Oliphant</option>
          <option value="sutherland">Sutherland</option>
          <option value="thiele">Thiele</option>
        </optgroup>

        </optgroup>


      </select>

      <label>Subjects</label>
      <input type="checkbox" id="development" value="interest_maths" name="user_interest">
      <label class="light" for="development">Maths C</label>
      <br>
      <input type="checkbox" id="design" value="interest_physics" name="user_interest">
      <label class="light" for="design">Physics</label>
      <br>
      <input type="checkbox" id="business" value="interest_chemistry" name="user_interest">
      <label class="light" for="business">Chemistry</label>
      <br>
      <input type="checkbox" id="business" value="interest_biology" name="user_interest">
      <label class="light" for="business">Biology</label>
      <br>
      <input type="checkbox" id="business" value="interest_marine" name="user_interest">
      <label class="light" for="business">Marine Biology</label>
      <br>
      <input type="checkbox" id="business" value="interest_graphics" name="user_interest">
      <label class="light" for="business">Graphics</label>
      <br>
      <input type="checkbox" id="business" value="interest_ipt" name="user_interest">
      <label class="light" for="business">IPT</label>
      <br>
      <input type="checkbox" id="business" value="interest_its" name="user_interest">
      <label class="light" for="business">ITS</label>
      <br>
      <input type="checkbox" id="business" value="interest_business" name="user_interest">
      <label class="light" for="business">Business</label>
      <br>
      <input type="checkbox" id="business" value="interest_legal" name="user_interest">
      <label class="light" for="business">Legal Studies</label>
      <br>
      <input type="checkbox" id="business" value="interest_engineering" name="user_interest">
      <label class="light" for="business">Engineering</label>
      <br>
      <input type="checkbox" id="business" value="interest_accoutngin" name="user_interest">
      <label class="light" for="business">Accounting</label>


    </fieldset>


    <legend><span class="number">3</span> Submit!</legend>
    <input type="checkbox" id="update" value="interest_update" name="user_update">
      <label class="light" for="update">Tick to recieve a free complimentary Elliot</label>


    <button type="submit" onclick="displayOutput()">Submit!</button>

  </form>

</body>

3 个答案:

答案 0 :(得分:0)

只需列出所有值,并在它们之间添加'\n'即可添加换行符。

alert($("#name").val() + '\n' + $("#mail").val() + '\n' + 
      $("#password").val() + $(":radio[name=user_gender]:checked").val() + '\n' +
      $("#bio").val() + '\n' + 
      'Subjects: ' + $(":checkbox[name=user_interest]:checked").map(function() { return $(this).val(); }).join(','));

答案 1 :(得分:0)

要浏览表单的所有元素,您可以使用elements

   function displayOutput() {
      for(var i=0;i<document.forms[0].elements.length;i++)
        console.log(document.forms[0].elements[i].id +": " 
                +  document.forms[0].elements[i].value);
   }

这仅检查value,但其他人需要检查,例如:selected,但这并不复杂。

如果它太复杂,按照建议,请尝试以此为出发点:

   function displayOutput() {
      var id;
      var content;
      var elem;
      for(var i=0;i<document.forms[0].elements.length;i++){
        id = document.forms[0].elements[i].id;
        if(!id)
          continue;
        elem = document.getElementById(id);
        if(elem.tagName === "INPUT" || elem.tagName === "TEXTAREA"){
          if(elem.type === "checkbox")
            content = (elem.checked)?"checked":"blank";
          else if(elem.type === "radio")
            content = (elem.checked)?"checked":"blank";
          else
            content = elem.value;
        } else if(elem.tagName === "SELECT"){
          content = elem.options[elem.selectedIndex].value;
        }
        console.log(id +": " + content);
     }
   }

我花了几分钟才发现你有多个ID(每个文档只能有一个id

答案 2 :(得分:0)

如果您的表单具有正确的ID,则可能会有所帮助:

var res = '';
$.each($('#formid').serializeArray(), function(i, v){
  res += i+':'+v+'\n';
});
alert(res);

当然,alert是在现实世界中这样做的可怕方式。