简体   繁体   English

jQuery每个函数在同一元素中具有多个选择器输出

[英]jQuery each function with multiple selectors output in same element

Hy guys ! 大家好!

I would like to display (with $.each) a summary of my form in a single element, like that : 我想在单个元素中显示(带有$ .each)我的表单的摘要,如下所示:

Lorem - 15$
Ipsum - 27$
Amet - 12$

But how to associate each selectors ? 但是如何关联每个选择器? : JSFiddle JSFiddle

Actually i got this output : 其实我得到了这个输出:

Lorem -
15 -
Ipsum -
27 -
Amet -
12 -

Thank you for all ;-) 感谢你所做的一切 ;-)

If your HTML format is fixed , you can use next to get the next input value. 如果您的HTML格式是固定的 ,则可以使用next获取下一个input值。

$.each($("input[name*='data']"), function() {
    $('#preview').append($(this).val() + ' - ' + $(this).next('input').val() + '$<br>');
});

DEMO 演示

Maybe you can try this: 也许您可以尝试以下方法:

var counter = 1,
    $preview = $('#preview');

$.each($("input[name*='data'],input[name*='price']"), function() {

    $preview.append($(this).val());

    if (counter % 2 === 0) {
        $preview.append('$ <br>');
    } else {
        $preview.append(' - ');
    }

    counter++; 
});

In each iteration you check if you are on even, or odd item, and you can define your own ending. 在每次迭代中,您都检查是偶数还是奇数项,然后可以定义自己的结尾。 This is more universal solution. 这是更通用的解决方案。

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM