[英]option in SELECT tag with multiple values?
Can I use 'select' tag like below to have multiple values for options? 我可以使用如下所示的“ select”标签为选项提供多个值吗?
<select name="data">
<option value="{'name':'foo','type':'bar'}">Option one</option>
<option value="{'name':'foo2','type':'bar'}">Option two</option>
</select>
If so, then how can I access the values using javascript? 如果是这样,那么如何使用javascript访问值?
The value is always one, but if you serialize a JSON object, as you seem you are trying to do, inside the value
attribute, you can retrieve it later using JSON.parse()
, like so: 该值始终为1,但是如果您序列化一个JSON对象(看起来像是在尝试这样做),则可以在
value
属性中使用JSON.parse()
稍后检索它,如下所示:
jQuery(document).ready(function($){
$('select').on('change',function(){
var val = JSON.parse( $(this).val() );
console.log(val);
});
});
Warning: Your serialized object is not a valid JSON format , you should use double quotes instead of single ones. 警告:您的序列化对象不是有效的JSON格式 ,您应该使用双引号而不是单引号。
Give the select tag an ID. 给选择标签一个ID。 And then, access it via javascript with “.value”
然后,使用“ .value”通过javascript访问它
<script>
function getValue() {
var choice = document.getElementById('myTag').value;
console.log(choice);
}
</script>
<select name="data" id="myTag">
<option value="{'name':'foo','type':'bar'}">Option one</option>
<option value="{'name':'foo2','type':'bar'}">Option two</option>
</select>
<br>
<input type="button" value="Get Data" onClick="getValue()">
<select name="data" id='dropdown'>
<!-- ensure you're using proper json formatting here.
*strings are enclosed with double quotes.* -->
<option value='{"name":"foo","type":"bar"}'>Option one</option>
<option value='{"name":"foo2","type":"bar"}'>Option two</option>
</select>
<script>
//you could use something like this in a
//callback when you're ready to read the values.
var userSelection = JSON.parse( document.getElementById('dropdown').value );
/*
* the JSON.parse call was important because the value will
* initially be returned as a string in JSON format; But we
* want an object instead, so we can access its values with
* dot syntax for ease of use, so we use that to convert it.
*/
var selectedName = userSelection.name;
var selectedType = userSelection.type;
</script>
You can always use non-standard attributes instead of value
, then access them in a similar way as you would if you were using value
. 您始终可以使用非标准属性代替
value
,然后以与使用value
类似的方式访问它们。 For example, 例如,
<select name="data">
<option name="foo" type="bar">Option one</option>
<option name="foo2 type="bar">Option two</option>
</select>
I'm not sure how or where you want to access them, as you didn't say, but I'll give an example: 我不确定您要访问它们的方式或位置,就像您没有说的那样,但是我举一个例子:
newData = $("[name='data']").filter(":selected").attr("name");
dataType = $("[name='data']").filter(":selected").attr("type");
Of course, to be standards-compliant, the new attributes should start with data-
, and name
means something special in form
elements, but you get the gist. 当然,为了符合标准,新属性应以
data-
开头,并且name
表示form
元素中的某些特殊内容,但您可以理解要点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.