如何使用jquery/javascript计算和显示表单中键入的字符数

how to count and display numbers of characters typed in a form using jquery/javascript.?

本文关键字:字符 表单 显示 jquery 何使用 javascript 计算      更新时间:2023-09-26

我正试图建立一个计算其中键入的字符总数的表单。我的表单包含姓名、地址和客户的基本信息等字段。我想要得到所有字段中键入的字符总数。我该怎么做?'

HTML代码

//

<form name="form_3" id="form_3" method="post" action="action_form_2.php">
    <table border="1" width="1300" height="200" bordercolour="BLUE" >
            <tr>
            <td>Name and Address of Company:: <textarea name="name_address1" cols="30" rows="6" ></textarea><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Years In job::<input type="text" name="years_job">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                &nbsp;&nbsp;&nbsp;Postion/Type of Business::&nbsp;&nbsp;<input type="text" name="pos_type">&nbsp;&nbsp;Years Employed in this profession::<input type="text" name="years_employed" id="years_emp">
                Business Phone::<input type="text" name="phone_no" id="phone_no" >
                <input type="checkbox" name="self_employed" id="self_employed" value="self">Self Employed<br>
            </td>
           <td>Name and Address of Company:: <textarea name="name_address2" cols="30" rows="6" ></textarea><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Years In job::<input type="text" name="years">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
               &nbsp; Postion/Type of Business::&nbsp;&nbsp;<input type="text" name="type" >&nbsp;&nbsp;Years Employed in this profession::<input type="text" name="years_emp">
                 Business Phone::<input type="text" name="phone" id="phone"   >
                <input type="checkbox" name="self_employed" id="self_employed" value="self">Self Employed<br>
            </td>
            </tr>
             </table>
              <h4>iv.EMPLOYMENT INFORMATION</h4> 
           <table border="1" width="1300" height="200">
            <tr>
            <td>Name and Address of Company:: <textarea name="name_address3" cols="30" rows="6" ></textarea><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Years In job::<input type="text" name="years" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Position/Type of job::<input type="text" name="positions" ><br>
                &nbsp;&nbsp;&nbsp;Date(frm-to)::&nbsp;&nbsp;<input type="text" name="date" >&nbsp;&nbsp;Monthly Income::<input type="text" name="date" >
                  Business Phone,inc area code::<input type="text" name="phone" >
                <input type="checkbox" name="self_employed" id="self_employed" value="self"  >Self Employed<br>
            </td>
            <td>Name and Address of Company:: <textarea name="name_address4" cols="30" rows="6" ></textarea><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Years In job::<input type="text" name="years">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Position/Type of job::<input type="text" name="positions"><br>
                &nbsp;&nbsp;&nbsp;Date(frm-to)::&nbsp;&nbsp;<input type="text" name="date">&nbsp;&nbsp;Monthly Income::<input type="text" name="date">
                  Business Phone,inc area code::<input type="text" name="phone" >
                <input type="checkbox" name="self_employed" id="self_employed" value="self">Self Employed<br>
            </td>
            </tr>
            <tr>
              <td>Name and Address of Company:: <textarea name="name_address5" cols="30" rows="6" ></textarea><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Years In job::<input type="text" name="years">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Position/Type of job::<input type="text" name="positions"><br>
                &nbsp;&nbsp;&nbsp;Date(frm-to)::&nbsp;&nbsp;<input type="text" name="date">&nbsp;&nbsp;Monthly Income::<input type="text" name="date">
                  Business Phone,inc area code::<input type="text" name="phone">
                <input type="checkbox" name="self_employed" id="self_employed" value="self">Self Employed<br>
            </td>
             <td>Name and Address of Company:: <textarea name="name_address" cols="30" rows="6" ></textarea><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                Years In job::<input type="text" name="years">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Position/Type of job::<input type="text" name="positions"><br>
                &nbsp;&nbsp;&nbsp;Date(frm-to)::&nbsp;&nbsp;<input type="text" name="date">&nbsp;&nbsp;Monthly Income::<input type="text" name="date">
                  Business Phone,inc area code::<input type="text" name="phone">
                <input type="checkbox" name="self_employed" id="self_employed" value="self">Self Employed<br>
            </td>
            </tr>
             </table>  
        <h4>MONTHLY INCOME AND COMBINED HOUSING EXPENSE INFORMATION</h4>

            <table border="1"  width="1300" height="150">
              <tr>
                <td>Gross monthly income</td>
                <td>Borrower</td>
                <td>Co-borrower</td>
                <td>Total</td>
                <td>Combined Monthly Housing Exp.</td>
                <td>Present</td>
                <td>Proposed</td>
            </tr>
             <tr>
             <td>Base EMPL income<</td>
             <td>$::<input type="text" name="empl_income" id="empl_income"></td>
             <td>$::<input type="text" name="co_borrower" ></td>
             <td>$::<input type="text" name="base_empl_total"></td>
                <td>Rent</td>
                <td>$::<input type="text" name=""></td>
                 <td>$::<input type="text" name="years"></td>
             </tr>
               <tr>
             <td>Overtime</td>
             <td>$::<input type="text" name="overtime_borrower"></td>
             <td>$::<input type="text" name="overtime_coborrower"></td>
              <td>$::<input type="text" name="overtime_total"></td>
             <td>Mortgaged</td>
             <td>$::<input type="text" name="years"></td>
              <td>$::<input type="text" name="years"></td>
             </tr>
             </table>
       <br>
       <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;        <input type="submit" name="submit" value="SUBMIT" onclick="return validateform();">

    </body>
</html>

只需遍历包含输入文本的所有表单字段并计算其长度:

$('#yourform').on('submit', function(){
  var count = 0;
  $('input[type="text"], textarea', this).each(function(){
    count += $(this).val().length;
  });
  // your total is in count variable
  return false; // or true to submit form
});

我把它放入jsFiddle与你的HTML: http://jsfiddle.net/MUs7s/