如何隐藏附加输入名称时该值为空 [英] How to hide appended input names when the value is empty

查看:199
本文介绍了如何隐藏附加输入名称时该值为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个小脚本,他们的名字变成一个textarea沿追加输入值的列表。我想要做的是隐藏字段的名称时,该值为 0

Hi all, I have a small script that appends a list of input values along with their names into a textarea. What I'd like to do is hide the field name when the value is 0 or empty.

下面的脚本:

<script>
  function showValues() {
    var fields = $(".content :input").serializeArray();
    $("#contentlist_copy").empty();
    jQuery.each(fields, function(i, field){
      $("#contentlist_copy").append(field.value + " " + field.name + ", ");
    });
  } 
  $("input").change(showValues);
  showValues();     
</script>

我知道这是一个基本的问题,我AP preciate你的帮助。

I know it's an elementary question and I appreciate your help.

推荐答案

我认为你应该使用 .VAL()方法来设置一个textarea的值。您也可以使用 jQuery.map()方法来拼凑一个字符串数组,然后加入他们的行列。如果在 jQuery.map()方法回调函数返回null或undefined,该项目没有添加到阵列。

I think you should use the .val() method to set the value of a textarea. You can also use the jQuery.map() method to put together an array of strings and then join them. If the callback function for the jQuery.map() method returns null or undefined, the item is not added to the array.

function showValues() {
    var fields = $(".content :input").serializeArray();
    var tokens = jQuery.map(fields, function(field) {
            return (field.value && field.value != '0') ? (field.value + ' ' + field.name) : null;
        });
    $("#contentlist_copy").val(tokens.join(', '));
}

更新:

在code以上试图保持很多你原来的code,但你真的不需要调用 .serializeArray()。在评论你问有关使用标题,而不是名。下面code这是否:

The code above tried to maintain much of your original code, but you really don't need to call .serializeArray(). In the comments you asked about using the "title" instead of the "name". The following code does that:

function showValues() {
    var tokens = [];
    $('.content :input').each(function() {
        var $input = $(this);
        if (($input.val() != '') && ($input.val() != '0')) {
            tokens.push($input.val() + ' ' + $input.attr('title'));
        }
    });
    $('#contentlist_copy').val(tokens.join(', '));
}

这篇关于如何隐藏附加输入名称时该值为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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