


JS adds and deletes a group of text boxes and verifies the input information to determine its correctness_javascript skills
We encountered such a problem during the project, that is, we need to add several sets of data to the database, but the specific sets of data are not sure and have to be filled in by customers. For example, we need to add a discount strategy. There may be many sets of plans for each strategy, such as " 50% off for purchases over 100, 40% off for purchases over 200, 30% off for purchases over 500, etc. This is implemented as a set of plans, but it is not certain how many sub-plans there are in a set of plans, so here I use JS Add and delete sub-schemes, and judge the correctness of the scheme input, and write it to the database through json transmission. Here we mainly write how to add and delete a group of sub-projects and how to add verification to each text box.
Dynamicly add a group of text boxes:
var countTable = 0;
//Add table rows
addTable = function () {
//Get the table
var tab1 = document.getElementById("discountTable");
// The number of all cells in the table
// cell = tab1.cells.length;
//The number of rows in the table
n = tab1.rows.length;
//The number of columns in the table
//cell = cell / n;
//Add a row to the table
r = tab1.insertRow(n);
//Add each cell of the current row
r. insertCell(0).innerHTML = 'Spend X yuan: ';
r.insertCell(1).innerHTML = 'Discount rate:';
r.insertCell(2).innerHTML = '';
countTable = countTable 1;
}
Note:
1. The countTable here should be all variables, used to identify each row, so as to ensure that each row is different and prevent duplicate IDs after deleting a row. Condition.
2. A focus leaving event is added here for each text, that is, when the focus leaves the current text box, we need to determine whether it matches the input.
3. A label is added after the text box as a verification control. When the text we enter does not meet the requirements, the label will be visible.
Delete any line:
//Delete the current line
deleteTable = function (el) {
// alert( el.id);
//Get table
var tab1 = document.getElementById("discountTable");
//Loop Determine the rows that need to be deleted
for (i = 0; i //Get the ID of the row
var deleteValue = tab1.rows[i].cells[ 2].childNodes[0].id;
//Loop to get the id of each row and compare it with the currently clicked ID. If they are the same, delete them
if (el.id == deleteValue) {
tab1.deleteRow (i);
break; One line is the line in the current point, and then deleted.
How to show and hide validation controls (judge the text when the focus leaves the text):
Copy code
objText.parentNode.children[1].style.display="block";
return;
}
//Information Must be a number
if (isNaN(terifyText))
{
objText.parentNode.children[1].style.display = "block";
return;
}
objText .parentNode.children[1].style.display = "none";
}
When all information needs to be written, we also need to make a judgment. If there is an illegal prompt, Otherwise, the datatable is generated and returned. The specific method of transmitting it to the background will be written in the next blog.
//Generate DataTable object
function generateDtb() {
//Determine whether the data can be written to the flag, false means it can be written, true means it cannot be written
var flag = false;
//Get table
var tab1 = document.getElementById("discountTable");
//First column data
var firstGroup = document.getElementsByClassName("groupFirst");
//Second column data
var secondGroup = document.getElementsByClassName("groupSecond");
//Determine whether the verification information is legal
var veritify = document.getElementsByClassName("veritifyMessage");
// alert(secondGroup.item(0).value);
//Determine whether it is empty
for (var i = 0; i {
/ /Determine whether the first column of data is empty. If it is empty, display the prompt
if (firstGroup[i].value == "")
{
veritify[(i * 2)].style.display = "block";
flag = true;
}
//Determine whether the second column of data is empty. If it is empty, a prompt will be displayed
if (secondGroup[i].value == "" )
{
veritify[(i * 2 1)].style.display = "block";
flag = true;
}
}
for (var i = 0 ; i {
if (veritify[i].style.display == "block")
{
flag = true;
}
}
// alert(veritify.length);
//Any information entered is legal, then the current information is organized into an array and the information is returned for processing.
if (flag == false) {
//Write
var txtName = document.getElementById("txtName").value;
//Create array
var dtb = new Array ();
//Write data to the array through loop and return
for (var i = 0; i var row = new Object();
row.Name = txtName;
row.fullMoney = firstGroup[i].value;
row.discount = secondGroup[i].value;
dtb.push(row);
}
return dtb;
}
The verification here is also relatively simple. It just verifies whether the text box input is empty and whether it is a number, and uses the display and hiding of a label to determine whether According to the input, I will write in the next article how to pass the array into the background and write it to the database.

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!
