jQuery来填充隐藏的输入,但保留现有的内容 [英] jQuery to populate hidden input but retain existing content

查看:133
本文介绍了jQuery来填充隐藏的输入,但保留现有的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来填充一个隐藏的输入表单,其中包含页面上所有复选框的值,但是我也想保留复选框的预填充值,但无法解决如何执行此操作。

I have the following code to populate a hidden input form with the values of all the checkboxes on a page, however I also want to retain the prepopulated values of the checkbox but can't work out how to do this.

这里是我目前的代码,任何帮助都将非常感谢!

Here's my current code, any help would be most appreciated!

$(function(){

  function Populate(){
  vals = $('input[type="checkbox"]:checked').map(function() {
      return this.value;
  }).get().join(', ');
  console.log(vals);
  $('#hidden_form_id').val(vals);
  }

  $('input[type="checkbox"]').on('change', function() {
  Populate()
  }).change();

});


推荐答案

您可以修改您的语句

更改

$('#hidden_form_id').val(vals);

$('#hidden_form_id').val($('#hidden_form_id').val() + vals);

您可以使用 + = operator 来保留使用javascript的现有值

You may use += operator to retain existing value using javascript

$('#hidden_form_id')[0].value += vals;

您的代码为

You code would be

 function Populate(){
  vals = $('input[type="checkbox"]:checked').map(function() {
      return this.value;
  }).get().join(', ');
  console.log(vals);
  $('#hidden_form_id')[0].value += vals;
  }

这篇关于jQuery来填充隐藏的输入,但保留现有的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆