JS dynamic addition option case analysis, js dynamic Case Analysis

Source: Internet
Author: User

JS dynamic addition option case analysis, js dynamic Case Analysis

This article analyzes how to add options dynamically in JS. We will share this with you for your reference. The details are as follows:

I. Problems:

The number of questions and answers of a questionnaire is not fixed.

JS control answer

II. Implementation Method:

To achieve this effect, click the button to add 4 answer boxes below, select the 26 letters from the A-Z, is to facilitate the processing of ASSIC code

View HTML results

<Table width = "100%" class = "form"> <tr> <th width = "100px"> <label> Title 1: </label> </th> <td align = "left"> <input type = "text" class = "large" id = "title" name = "title"/> <span class = "error"> enter the title </span> </td> </tr> <th width = "100px"> <label> select a type: </label> </th> <td align = "left"> <input type = "radio" name = "radio"/> Single-choice <input type = "radio" name = "boxcheck"/> select multiple <input type = "radio" name = "select"/> drop-down list </td> </tr> <th width = "100px"> <label> answer: </label> </th> <td align = "left"> <div style = "float: left;" id = "1">: <input type = "text" class = "mimi" name = "A"/> B: <input type = "text" class = "mimi" name = "B"/> C: <input type = "text" class = "mimi" name = "C"/> D: <input type = "text" class = "mimi" name = "D"/> </div> <div class = "more" onclick = "add_ask ($ (this )) "> </div> </td> </tr> </table>

The above HTML result is then implemented through JS

Function add_ask ($ this) {var $ Word = ""; // obtain the front div layer var $ div_num = $ this. prev (). children (). attr ("id"); // Add layer var $ div_next_num = Number ($ div_num) + 1; var $ last_children_name = $ this. prev (). children (). last (). children (). last (). attr ("name "). charCodeAt (); var $ html = "<div style = \" float: left; \ "id =" + $ div_next_num + "> "; // perform four cycles if ($ last_children_name + 4 <= 90) {for (var $ I = 1; $ I <= 4; $ I ++) {$ Word = String. fromCharCode ($ last_children_name + $ I); $ html + = $ Word + ": <input type = \ "text \" class = \ "mimi \" name = "+ $ Word +"/> ";}} else {$ end = 90-$ last_children_name; for (var $ I = 1; $ I <= $ end; $ I ++) {$ Word = String. fromCharCode ($ last_children_name + $ I); $ html + = $ Word + ": <input type = \ "text \" class = \ "mimi \" name = "+ $ Word +"/> ";}} $ html + = "</div>"; $ this. prev (). append ($ html );}

The above JS is implemented through jquery. The principle is very simple, so I won't say too much.

The clever thing is to add options through assic code digital conversion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.