문제

I hve this HTML:

<input type="text" id="S1" />
<input type="text" id="S2" />
<input type="text" id="S3" />
<input type="text" id="S4" /> 
<input type="text" id="S5" />

How can I achieve this pseudo-code:

var qData{};
for (i = 1; i == 5; i++) {
        qData.qBidS + i = $("#S" + i).val;
    }

At the end I want is a set of 5 qData: qData.S1, qData.S2, qData.S3, qData.S4, qData.S5

도움이 되었습니까?

해결책

Use

var qData = {};
for(i = 1; i <= 5; i++) {
    qData["qBidS" + i] = $("#S" + i).val();
}

Problems:

  • Use .val(), there is no value property of jQuery object.
  • Use Bracket Notation and use string as per your requirement.
  • Condition should be i <= 5 not i == 5

다른 팁

Demo Fiddle

jQuery

$('#textBoxCover input').each(function () {
     qData.push($(this).val());
});  

Hope it helps..!!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top