php option value数组,如何将当前选项值转换为数组?(How do I get the current option values into an array?)...

如何将当前选项值转换为数组?(How do I get the current option values into an array?)

假设我有一个列表:

Horse

Bird

Dogs

Cats

我知道如何通过循环来获取每个选项的值。 我的问题是我有代码,如果满足某些条件,将默认值更改为某些条件,但如果条件不满足,我需要恢复为DEFAULT值。 我需要将默认值放入一个数组中,这样我就可以使用它们在条件不满足时恢复...等等。 我需要纯粹的javascript没有框架。

例如,如果我将Horse更改为Train我需要能够存储Horse在它被更改之前,您可以获得输入字段的默认值,以便在需要时还原为它。

Suppose I have a list:

Horse

Bird

Dogs

Cats

I know how to grab the values of each option by looping through. My problem is I have code that changes the default values to something if certain conditions are met but if the conditions are not met I need to revert back to the DEFAULT values. I need to get the default values into an array so I can use them to revert back if the conditions are not met...etc. I need pure javascript no frameworks.

So for example if I change Horseto TrainI need to be able to store Horse BEFORE it gets changed the same way you can get the default value for an input field to revert back to it if needed.

原文:https://stackoverflow.com/questions/34713244

更新时间:2020-02-23 19:58

最满意答案

使用以下代码:

var options = document.querySelectorAll("option");

var optionsArray = Array.prototype.map.call(options, function(x) {

return x.value;

});

Horse

Bird

Dogs

Cats

I figured out the solution and that is to get the values on page load and make sure your script is before the closing body tag.

2016-01-11

相关问答

我为自己找到答案,但如果有人能发布更好的答案,请继续。 function updateDynamicEnum(field, subfield, child_strings){

if(document.getElementById(subfield) != null){

var de_key = document.getElementById(field).value;

var child = document.getElementById(subfield);

...

使用对象来存储键值对。 var images = {

"Sports 1" :

"http://lorempixel.com/50/50/sports/1",

"Sports 2":

"http://lorempixel.com/50/50/sports/2",

"Sports 3":

"http://lorempixel.com/50/50/sports/3",

};

var index, len;

var keys = Object.keys(image

...

你在找这样的东西吗? 我已更新您的数据以保存地点的价格和名称。 现在,选择将文本作为地点,将价格作为值。 更新小提琴 var select = document.getElementById("selectDestinasjon");

var options = [{"place": "Konsberg","price":"$20"}, {"place":"Trysil","price":"$30"}, {"place":"Beitostølen","price":"$40"}];

for(var

...

你正在寻找这个......

op1

op2

op3

var options = $('#foo')

...

请尝试此代码,在您的代码中,您在foreach循环中包含 ,这就是您获得3个选择标记的原因。 <?php

$names=array("jan","feb","mar");

echo "";

foreach($month_names as $mn)

{

echo "$mn";

}

echo "";

Please try this code, In your code you were in

...

好的,所以我想我猜到了你想说的。 你不应该在list.php文件中硬编码标签,而是在那里有一个数组。 这样,您既可以将其用于生成选择字段,也可以用于验证。 但是,通常具有类别的数据库表将是更可取的。 路径/ list.php的 return array(

'1' => 'Name of Ctg 1',

'2' => 'Name of Ctg 2'

);

在这里生成选择

$options = include('path

...

在代码中需要更改一些东西。 只要你知道它是有效的,你就不需要评估JSON。 一个标签没有“文本”属性。 在jQuery中,您应该使用$('.selector').text()更改标签的内部文本 您的JSON对象具有Titel和Produktid作为属性,而不是文本和值。 请小心......您的标签具有与您在$('#mySelect').append(newOption);指定的不同的id属性$('#mySelect').append(newOption); (您的选

...

使用以下代码: var options = document.querySelectorAll("option");

var optionsArray = Array.prototype.map.call(options, function(x) {

return x.value;

});

Horse

Bird

...

试试这个: foreach($email_hooks as $key=>$val){

echo $val['name'];

}

你的选择框:

-- Select One --

foreach($email_hooks as $key=>$val){

echo ''.$val['name'].'';

}

?>

try thi

...

WebElement drop_down =driver.findElement(By.id("cityID"));

Select se = new Select(drop_down);

List options = se.getOptions();

然后你可以迭代这个列表并执行.getText(); WebElement drop_down =driver.findElement(By.id("cityID"));

Select se = new Select(drop

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值