简体   繁体   English

如何为动态下拉选择分配ID?

[英]How to assign an ID to a dynamic dropdown select?

I want to assign a session ID (this will be hidden) whenever the first select dropdown is used. 每当使用第一个选择下拉菜单时,我都想分配一个会话ID(将被隐藏)。 The session ID is the number 300, 301, etc. after the "day". 会话ID是“天”之后的数字300、301等。

When a user selects something from the "preferred" dropdown it should assign the "300" value to the "sessionid" dropdown and simultaneously change the value of the "day" dropdown (which works already btw) 当用户从“首选”下拉列表中选择某项内容时,应将“ 300”值分配给“ sessionid”下拉列表,并同时更改“天”下拉列表的值(已生效)

How do I do implement it properly? 如何正确实施?

Please see code below: 请参见下面的代码:

<select id="preferred" class="preferred" name="preferred">
  <option value="">- Select -</option>
  <option value="Mountain Dew">Mountain Dew</option>
  <option value="Seven Up">Seven Up</option>
</select>
<select id="day" class="day" name="day">
  <option value=""></option>
</select>
<select id="sessionid" class="sessionid" name="sessionid">
  <option value=""></option>
</select>

$(function() {
  var selectValues = {
   "Mountain Dew": {
   "Monday": "300"
  },
   "Seven Up": {
   "Tuesday": "301"
  }
};

var $preferred = $('select.preferred');
var $day = $('select.day');
var $sessionid = $('select.sessionid');

$preferred.change(function() {
  $day.empty().append(function() {
   var output = '';
   $.each(selectValues[$preferred.val()], function(key, value) {
   output += '<option value="' + key + '">' + key + '</option>';
  });
  return output;
  });
 }).change();
});

example : http://jsfiddle.net/creativemix/5ZWrE/2/ 例如: http : //jsfiddle.net/creativemix/5ZWrE/2/

You can use Element.id = 'id' to set an id in javascript. 您可以使用Element.id = 'id'在javascript中设置ID。

https://developer.mozilla.org/en-US/docs/Web/API/Element.id https://developer.mozilla.org/zh-CN/docs/Web/API/Element.id

In your code that would probably be something like this at the point where you want to assign the id: 在您的代码中,在您想要分配ID的位置上可能是这样的:

$sessionid[0].id = $day.val();

But please note an id must start with a letter and numbers are generally bad practice even if it does work. 但是请注意,ID必须以字母开头,数字即使有效也通常是不正确的做法。

Putting the value you need in a hidden field is probably preferable over this solution. 将您需要的值放在隐藏字段中可能比此解决方案更可取。

Try this if you want to set the value of session id dropdown. 如果要设置会话ID下拉列表的值,请尝试此操作。 Include this after setting the output variable in your fiddle. 在您的小提琴中设置输出变量之后,请包括此内容。

$sessionid.empty()
     .append($("<option></option>")
     .attr("value",value)
     .text(value));

Here's a link to the fiddle. 这是小提琴的链接

But, as others already suggested, why not use a hidden text field to store the value? 但是,正如其他人已经建议的那样,为什么不使用隐藏的文本字段来存储值呢?

To store the value in a hidden field, 要将值存储在隐藏字段中,

<input type="hidden" id="hdnSessionId" />

And add this to the script. 并将其添加到脚本中。

$('#hdnSessionId').val(value);

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

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