I am creating a task management application using CakePHP.
I am trying to implement a function to change all task names at once.
PurposeIn jQuery, the character string (task name) in each text box and the data attribute (task id) set in each text box I want to get an associative array with a key and value relationship.
※ Finally, send the acquired data to the server via Ajax communication and pass the value to Model.
I want to apply UPDATE processing by applying values to the following SQL statement.
UPDATE table_name SET name = acquired task name WHERE id = acquired task id;
Applicable source codeThe screen for changing the task name is slightly simplified.
<body>
<ul>
<li>
<input type = "text" data-task-id = "10">
</li>
<li>
<input type = "text" data-task-id = "20">
</li>
<li>
<input type = "text" data-task-id = "30">
</li>
</ul>
<button>PUSH</button>
</body>
$(function () {
// Get the data attribute of the list
$(document) .on ('click', '# submit', function () {
let tasklist = [];
$('# tasklist input'). each (function () {
// Get the data attribute of the input tag
let id = $(this) .data ('task-id');
// Get the string of the input tag (in the text box)
let name = $(this) .val ();
tasklist [id] = name;
});
console.log (tasklist);
});
});
Problem
In the above source code,Character string entered in text boxandeach set data attribute valueare obtained associative array key, value It is in a ready state.
You can use either key or value, but if key becomes a number,value from 0 to that numberwill be defined as empty.
In the above case, the task-id of the data attribute is set as the key, but the key 0 ~ 9, then 11 ~ 19 etc. will be defined as empty. ↓
* Console display when a, b, c are entered in the text box
(31) [empty × 10, "a", empty × 9, "b", empty × 9, "c"]
10: "a"
20: "b"
30: "c"
length: 31
proto: Array (0)
・ I checked the number of times that it turned around in $.each of jQuery, but it was 3 times.
・ The values of the defined variables "id" and "name" were successfully displayed on the console in $.each.
・ At the bottom of $.each, when the variable tasklist is displayed on the console, it becomes the following.
(11) [empty × 10, "a"]
(21) [empty × 10, "a", empty × 9, "b"]
(31) [empty × 10, "a", empty × 9, "b", empty × 9, "c"]
windows10
PHP ver7.3.8
CakePHP ver3.8
or more.
-
Answer # 1
Related articles
- php i want to judge whether a character string with a specific pattern exists and store it in a variable if it exists
- [php] how to convert from character string to date type
- php - i want to delete a character string that precedes a specific character (appears multiple times)
- php - output of specified character in character string
- php - about button reference in jquery
- [javascript] i want to copy the character string embedded in html to the clipboard
- i want to display a character string with a different font in qtablewidgetitem
- how to output the character string read from the csv file to pdf
- jquery - about the attribute value of the alt attribute
- php - should the string be enclosed in single or double quotes?
- vbnet - why is it converted to a character string and processed as a blank?
- when an email with a specific character string in the subject of outlook vba arrives, write the text to excel
- i want to remove the part of the php string before /
- java - i want to take out the character string in arraylist and change it to a number
- vuejs - to bind by adding the character "" in vue attribute binding
- typescript - i want to automatically set a character string in ion-input with ionic × protractor
- php - about changing the character code
- php - about truncation of format string
- vba - i want to search for the character string entered in the user form and paste the cell
- php - the attribute indicating the parent-child relationship does not appear on the edit screen of the custom post
- javascript : How to sort the html table by column at once, creating it from an array?
- javascript : JS, HTML: How to get data from a form if there are several of them and they have the same class
- javascript : How to pass variable value from JS to php via hidden variable
- javascript : Is it possible to specify multiple clsss of form->error?
- javascript : Encoding consisting of /letters and numbers
- javascript : Array sort selection
- javascript : navigator.clipboard.writeText () doesn't work
- javascript : How to get the size of the browser window using js or python and transfer this size to the css file?
- javascript : How to keep the scroll level in the chat while focusing on the input on the mobile version?
- javascript : How do I capitalize the first and last letter?
First of all, JavaScript doesn't (strictly) have associative arrays.
If
tasklist [id] = name;
contains a numeric value 10, the name is assigned to the 10th array element. (0-9 are undefined array elements)or