Populate var with value from dropdown

Currently I have a script that when a user selects an item from 4 different dropdowns with the same class it populates an input field with the values. $('.neigh').on('change',function() { selectedItems = new Array(); //console.log(selectedItems); $(".neigh option[value]:selected").each(function() { if($.inArray($(this).val(), selectedItems) != 0){ selectedItems.push($(this).val()); } }); //console.log(selectedItems); var data = selectedItems.join(','); $(".hood").val(data); }); I would like to modify this so that instead of it appending the value with a ',' to completely empty the array and add the new value. How would I go about doing that to this current script? EDIT: So the result would be if user chose option 1 and then option 2. Option 2 will only be within data. EDIT 2: There are 4 dropdowns with multiple options. Each dropdown is linked by the same class to this function. Previously it appended each choice to one input box. So if a user selected mutliple options the hidden input box would contain "24, 24, 6, 7" etc. Instead of this I would rather just the most recent selection. So if a user selects 24 the value in the hidden input would be 24, but if they select another option in a different dropdown it will now contain the new value.

以上就是Populate var with value from dropdown的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏