| |

VerySource

 Forgot password?
 Register
Search
View: 884|Reply: 7

How to get all the values ​​of all select controls

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-19 16:40:01
| Show all posts |Read mode
Such as the title: This is the code I wrote. The master shows me how to assign all the values ​​of the js script to the select control. I got it in the background, thanks.
<tr>
   <td align = "right" valign = "top"> <font color = # FF6600> * </ font> <b> </ b> Working place </ td>
   <td> <select name = "selloc1" size = "5" multiple id = "selloc1" runat = server style = "width: 160">
        <option value = '# 5 #'> Beijing / Beijing </ option
        </ select>
   </ td>
   <td> <INPUT TYPE = "BUTTON" NAME = "Input3" VALUE = "Add >>" OnClick = "JavaScript: additem (selloc1, selloc2)">
              <BR> <INPUT TYPE = "BUTTON" NAME = "Input22" VALUE = "<< Remove" OnClick = "JavaScript: delitem (selloc2)"> </ td>
   <td> <select name = "selloc2" size = "5" MULTIPLE id = "selloc2" style = "width: 160">
       </ select>
   </ td>
</ tr>

-------------------------- javascrip has been implemented without problems ------------------- ----
<script language = javascript>
<!-begin
function additem (sel1, sel2) {
  var flag;
  var count = 0;
  for (var x = 0; x <sel1.length; x ++) {
    var opt = sel1.options [x];
if (opt.selected) {
  count = count + 1;
      if (count> 5) {
  alert ("No more than 5 selected items!");
return;
  }
    }
  }
  if (count> 1&&sel1.options [0] .value == 0&&sel1.options [0] .selected) {
  alert ("You have selected [Unlimited] items, no other items can be selected!");
return;
  }

  for (var x = 0; x <sel1.length; x ++) {
    var opt = sel1.options [x];
    if (opt.selected) {
  flag = true;
  for (var y = 0; y <sel2.length; y ++) {
        var myopt = sel2.options [y];
        if (myopt.value == opt.value) {
          flag = false;
        }
else if (sel2.options [0] .value == 0) {
alert ("You have selected [Unlimited] items, no other items can be selected!");
return;
}
      }
      if (flag) {
if (sel1.options [0] .selected&&sel1.options [0] .value == 0) {
for (var x = sel2.length-1; x> = 0; x-) {
     sel2.options [x] = null;
    }
sel2.options [0] = new Option (opt.text, opt.value, 0, 0);
} else if (sel2.length> 4) {
  alert ("No more than 5 selected items!");
return;
    }

else {
sel2.options [sel2.options.length] = new Option (opt.text, opt.value, 0, 0);
}
      }
  
    }
  }
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-26 18:45:01
| Show all posts
Please elaborate, it is best to have an example.
Reply

Use magic Report

0

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-27 09:09:01
| Show all posts
Write to a hidden control.
Reply

Use magic Report

0

Threads

58

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-1-27 12:00:02
| Show all posts
Add a control:
<input type = "hidden" id = "selectvalues" runat = "server" />

Save all selected values ​​to the above control with a script

document.getElementById ("selectvalues"). value = document.getElementById ("selloc2) .options.join (", ") /*option.value or text, take a look again * /

In the background:
string [] options = this.selectvalues.value.Split (",")
foreach (string option ...)
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-1-27 14:18:02
| Show all posts
Due to the nature of the select control, the value of the select is lost when you submit to refresh the page.
You can use a TextBox control to assign the value in the select control to the TextBox, so that the value of the TextBox can be read in the background.
Reply

Use magic Report

0

Threads

73

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-27 20:27:02
| Show all posts
Select all the values ​​of selloc2 before submitting, and then use Request.Form ["selloc2"] to get all the values ​​in the background
Here is the JS with all the values ​​of selloc2 selected:
for (i = 0; i <document.all.selloc2.length; i ++) document.all.selloc2 [i] .selected = true;
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-27 08:30:01
| Show all posts
3rd floor positive solution
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-27 08:45:01
| Show all posts
Top, 3rd floor positive solution
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list