通过jQuery在文本输入中显示多个复选框值(从模式窗口) [英] display multiple checkbox values (from modal window) in text input, via jQuery

查看:32
本文介绍了通过jQuery在文本输入中显示多个复选框值(从模式窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑///////

除了下面的有用建议之外,事实证明,另一个错误是我在复选框名称属性中添加了"[]"括号.

In addition to the helpful advice below, it turns out that another bug was my inclusion of "[ ]" brackets inside my checkbox name attribute.

    <input type="checkbox" name="neighborhood_id[]"/>

那显然使我的jquery选择器搞砸了.

That was screwing up my jquery selector, apparently.

///////结束编辑

//////END EDIT

我已经经历了几个相关的话题,找到了答案,但都空了.我很肯定它在那里,但是它是如此简单,有人回答需要一分钟.

I've gone through several related threads to find an answer to this and have come up empty. I'm positive it's out there, but it's so simple, it should a minute for someone to answer it.

我想从多个选中的复选框(均具有相同的name属性)中获取值,并将其显示在文本输入中.不知道这是否将猴子扳手插入其中,但是复选框最初隐藏在模式窗口中.我的尝试正在奏效.我还向地图函数添加了.get()和toArray(),但没有成功.

I want to grab the values from multiple selected checkboxes (all with the same name attribute) and display them in a text input. Not sure if this is throwing a monkey wrench into it, but the checkboxes are initially hidden in a modal window. My attempt is working. I've also added .get() and toArray() to the map function, without success.

 $('[name=neighborhood_id[]]').change(function(){
    $('#area').val(function(){
        return $('[name=neighborhood_id[]]:checked').map(
            function(){return $(this).val()});
            });
    });

我遇到的问题是文本输入默认为以下内容:[object Object]并且当我检查复选框时它没有更新.

The problem that I'm having is that the text input is defaulting to the following: [object Object] and it's not updating when I do check the checboxes.

我假设这可能与从each/map函数作为数组Object返回值有关

I'm assuming this might be related to returning the values from the each/map function as an array Object

谢谢

推荐答案

您肯定在正确的轨道上,只需要进行一些更改:

You are definitely on the right track, just needed a few changes:

$('input[name=neighborhood_id]').change(function() {
  $('#area').val(
      $('[name=neighborhood_id]:checked').map(function() {
          return $(this).val();
      }).get().join(',')
 );

});

我认为您的主要问题是您正在向 $('#area').val

I think your main problem was that you were sending an anonymous function to $('#area').val

工作版本: http://jsfiddle.net/uZcaT/

这篇关于通过jQuery在文本输入中显示多个复选框值(从模式窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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