Enter each item in a certain form and move to the next page.
And when returning to the original page from the transition destination, the select box entered once and
You may want to redisplay the original value or the value already selected for the pull-down box.
This time for the pull-down item (option element) that dynamically acquires the value
The setting (selected) for the item selected when obtaining the display value or setting value is
I was unable to reflect it well, so I consulted you.
Use jquery to generate the display value and input value for the option element as shown below.
// Generate by specifying value and text
$("# select_test"). append ($("<option>"). val (hogeCd) .text (hogeName));
Although it was able to reflect so far, if you try to reflect the selected state further, it will not be reflected well.
Investigate
In html, I want to reflect this.
<select>
<option value = "1" selected = "selected">Option 1</option>
</select>
This
selected ="selected"
I want to reflect this part at the time of generating input value using jquery
// Generate by specifying value and text
$("# select_test"). append ($("<option>"). val (hogeCd) .text (hogeName));
Is there any way to reflect the "selected ="selected"" part of the description like this?
Thanks for your help. Thank you.
-
Answer # 1
-
Answer # 2
Sample code
<! DOCTYPE HTML> <html lang = "en"> <head> <meta charset = "UTF-8"> <title></title> </head> <body> <select name = "select"> </select> <script type = "text/javascript" src = "// code.jquery.com/jquery-3.1.1.min.js"></script> <script type = "text/javascript"> $(function () { var arr = { 0: 'text0', 1: 'text1', 2: 'text2', 3: 'text3' }; $.each (arr, function (i, row) { var option = $('<option>'); console.log (i); option.val (i) .text (row) .appendTo ($("[name = select]")); }); $("[name = select]"). val (3); }); </script> </body> </html>
Related articles
- javascript - i want to manage the checked state and unchecked state of multiple check boxes in react for each check box
- javascript - how to increase the pull-down value
- javascript - how to update and display the latest state value in a child component
- javascript - how to initialize state value when using react navigation
- javascript - to clear the child component's state in react
- i want to keep the state of the previous page when i press the back button with javascript (or jquery)
- javascript - how to change state of parent component from child component in react
- i don't know how to get the text in the range selection with javascript (jquery)
- javascript - state cannot be updated in react function component
- javascript - how to update react-redux state
- javascript - how to get prevstate of other state in usestate of react?
- javascript - i want to disable the selection box
- javascript - i want to display while preserving the state even if the page is refreshed with react
- javascript - i want to switch between a state where a check box can be selected and a state where it cannot be selected
- javascript - how to check if any file selection is selected
- javascript - i want to turn off the file selection explorer when dragging and dropping with input type = "file"
- javascript - [react] state is not reflected during asynchronous processing
- javascript - how to reset state in redux
- javascript - i want to insert the value of the item selected in the pull-down into the input tag
- javascript - how to change to the color selected in the pull-down menu
- javascript - jquery setinterval doesn't work
- javascript - i want to add the current display function of the current page
- javascript - buttons that scale the entire page do not work
- javascript - when overlaying transparent images, i want to cover both the horizontal and vertical positions perfectly
- i want to change the color of hover every time i click with javascript
- javascript - input screen like unlocking line
- javascript - i want to animate the characters that flow from left to right when reloaded
- javascript - i want to shuffle the characters based on the reference site
- java - the hamburger menu cannot be displayed properly
- javascript - [rails] css and js do not respond when material-dashboard is installed
When generating a DOM with $(), if you specify the second argument in the key: value format, you can attach an attribute, so you only need to add selected: true.
Result:
Text and val were also inserted.
[jQuery] Generation of DOM elements