简体   繁体   English

在HTML中保持键值对 <select/>用jQuery?

[英]Keeping key value pairs together in HTML <select/> with jQuery?

Given a select with multiple option's in jQuery. 给出jQuery中有多个选项的select。

$select = $("<select></select>");
$select.append("<option>Jason</option>") //Key = 1
       .append("<option>John</option>") //Key = 32
       .append("<option>Paul</option>") //Key = 423

How should the key be stored and retrieved? 如何存储和检索密钥?

The ID may be an OK place but would not be guaranteed unique if I had multiple select's sharing values (and other scenarios). ID可能是一个好的地方,但如果我有多个select的共享值(和其他场景),则不能保证唯一。

Thanks 谢谢

and in the spirit of TMTOWTDI. 并本着TMTOWTDI的精神。

$option = $("<option></option>");
$select = $("<select></select>");
$select.addOption = function(value,text){
    $(this).append($("<option/>").val(value).text(text));
};

$select.append($option.val(1).text("Jason").clone())
       .append("<option value=32>John</option>")
       .append($("<option/>").val(423).text("Paul"))
       .addOption("321","Lenny");

Like lucas said the value attribute is what you need. 就像卢卡斯所说,价值属性就是你所需要的。 Using your code it would look something like this ( I added an id attribute to the select to make it fit ): 使用你的代码看起来像这样(我在select中添加了一个id属性以使其适合):

$select = $('<select id="mySelect"></select>');
$select.append('<option value="1">Jason</option>') //Key = 1
   .append('<option value="32">John</option>') //Key = 32
   .append('<option value="423">Paul</option>') //Key = 423

jQuery lets you get the value using the val() method. jQuery允许您使用val()方法获取值。 Using it on the select tag you get the current selected option's value. 在select标签上使用它可以获得当前所选选项的值。

$( '#mySelect' ).val(); //Gets the value for the current selected option

$( '#mySelect > option' ).each( function( index, option ) {
    option.val(); //The value for each individual option
} );

Just in case, the .each method loops throught every element the query matched. 以防万一,.each方法遍历查询匹配的每个元素。

The HTML <option> tag has an attribute called "value", where you can store your key. HTML <option>标记有一个名为“value”的属性,您可以在其中存储密钥。

eg: 例如:

<option value=1>Jason</option>

I don't know how this will play with jQuery (I don't use it), but I hope this is helpful nonetheless. 我不知道这将如何与jQuery(我不使用它),但我希望这是有帮助的。

If you are using HTML5, you can use a custom data attribute . 如果您使用的是HTML5,则可以使用自定义数据属性 It would look like this: 它看起来像这样:

$select = $("<select></select>");
$select.append("<option data-key=\"1\">Jason</option>") //Key = 1
   .append("<option data-key=\"32\">John</option>") //Key = 32
   .append("<option data-key=\"423\">Paul</option>") //Key = 423

Then to get the selected key you could do: 然后,您可以执行以下选择的键:

var key = $('select option:selected').attr('data-key');

Or if you are using XHTML, then you can create a custom namespace. 或者,如果您使用的是XHTML,则可以创建自定义命名空间。

Since you say the keys can repeat, using the value attribute is probably not an option since then you wouldn't be able to tell which of the different options with the same value was selected on the form post. 既然你说密钥可以重复,那么使用value属性可能不是一个选项,因为那样你就无法分辨在表单帖子中选择了哪个具有相同值的不同选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM