1

I'm trying to concatenate a string from certain inputs in a form, and populate another input with that string. I've included my script below, and linked to a js fiddle.

I think the code inside the conditionals in the each() is too redundant, but I can't seem to make it work any other way. Any suggestions are appreciated.

var $namers = $(".namer");
$namers.on('change', function () {
    var length = $namers.length - 1;
    var nameString = "";
    $namers.each(function (i) {
        var delimiter = "";
        if ($(this).find(":selected").attr('value')) {
            if (i < length) delimiter = ": ";
            thisVal = $(this).find(":selected").text();
            nameString = nameString + thisVal + delimiter;
        } else if ($(this).is("input") && $(this).val()) {
            if (i < length && $(this).hasClass("from")) delimiter = "-";
            thisVal = $(this).val();
            nameString = nameString + thisVal + delimiter;
        }
    });
    $("#summary").val(nameString);
});

Here is my original: http://jsfiddle.net/3HsQW/

And a first stab at improving things using an array, which I'm not sure is much better. http://jsfiddle.net/3HsQW/1/

4

3 回答 3

0

我尝试了其他两个答案,但都没有奏效。我还了解到我应该使用http://codereview.stackexchange.com

我最终决定这样做(尽管我对这是否会帮助其他人有所保留):

var $namers = $(".namer");
$namers.on('change', function () {
    var length = $namers.length - 1;
    var nameString = "";
    var names = [];
    $namers.each(function (i) {
        var delimiter = "";
        if (i < length) delimiter = ": ";
        if ($(this).find(":selected").attr('value')) {
            names[i] = $(this).find(":selected").text() + delimiter;
        } else if ($(this).is("input") && $(this).val()) {
            if ($(this).hasClass("from")) delimiter = "-";
            names[i] = $(this).val() + delimiter;
        }
        if (names[i]) nameString += names[i];
    });
    $("#summary").val(nameString);
});
于 2013-01-17T03:01:53.203 回答
0

我还没有完全测试它,但这样的东西也应该可以工作:

var $namers = $('.namer');

$namers.on('change', function () {
    var nameString = '';

    $namers.each(function(i) {
        var $this = $(this);
        var $selected = $this.find(':selected');

        if ($selected.attr('value')) {
            nameString += $selected.val() + ': ';
        } else if ($this.is('input') && $this.val()) {
            nameString += $this.val() + $this.hasClass('from') ? '-' : '';
        }
    });

    $('#summary').val(nameString);
});

条件将i < length始终为false,因此您可以删除该代码。

于 2013-01-17T01:48:57.360 回答
0

这是 jquery 阻碍的罕见情况之一。我推荐类似的东西:

$("option").each( function() { this.value = $(this).text(); } );
$namers = $(".namer");
$names.on('change', function () {
    var length = $namers.length - 1;
    var nameString = "";
    $namers.each(function (i) {
        nameString += this.value;
        if (this.tagName == 'SELECT') nameString += ':';
        if (this.className == 'from') nameString += '-';
    });
    $("#summary").val(nameString);
});
于 2013-01-17T02:03:46.693 回答