admin管理员组

文章数量:1290394

I want to align the contents of 5 input fields (named "Textfield1" ... "Textfield5") on one page to the right by means of jQuery calling CSS. I tried this:

for (var i=1; i<=5; i++)
{
JQuery('#'+ff_getElementByName('"Textfield" + [i]').id).css('text-align', 'right');
}

and I tried this:

for (var i=1; i<=5; i++)
{
var2 = "Textfield" + [i];
JQuery('#'+ff_getElementByName('var2').id).css('text-align', 'right');
}

Neither one works. How to make it work? Thanks.

I want to align the contents of 5 input fields (named "Textfield1" ... "Textfield5") on one page to the right by means of jQuery calling CSS. I tried this:

for (var i=1; i<=5; i++)
{
JQuery('#'+ff_getElementByName('"Textfield" + [i]').id).css('text-align', 'right');
}

and I tried this:

for (var i=1; i<=5; i++)
{
var2 = "Textfield" + [i];
JQuery('#'+ff_getElementByName('var2').id).css('text-align', 'right');
}

Neither one works. How to make it work? Thanks.

Share Improve this question asked May 10, 2013 at 9:07 user2317194user2317194 2211 gold badge4 silver badges13 bronze badges 3
  • 1 Hey, what does your html look like? – WheretheresaWill Commented May 10, 2013 at 9:09
  • The html just assigns the name to an input field. The html is ok because JQuery('#'+ff_getElementByName('Textfield1').id).css('text-align', 'right'); works!! – user2317194 Commented May 10, 2013 at 9:11
  • Post the ff_getElementByName function also.. – palaѕн Commented May 10, 2013 at 9:11
Add a ment  | 

5 Answers 5

Reset to default 2

I think CSS will do this.

<input type="text" id="text1" class="align_text"/>
<input type="text" id="text2" class="align_text"/>
<input type="text" id="text3" class="align_text"/>
<input type="text" id="text4" class="align_text"/>
<input type="text" id="text5" class="align_text"/>
<input type="text" id="text6" class="align_text"/>

.align_text { text-align: right; }

The following code works, and here is a link to a demo: jsfiddle

for (var i=1; i<=5; i++)
{
  fields = "Textfield" + i;
  $('#' + fields).css('text-align', 'right');
}

This should work:

for (var i = 1; i <= 5; i++) {
    JQuery('#' + ff_getElementByName("Textfield" + i).id).css('text-align', 'right');
}

OR

for (var i = 1; i <= 5; i++) {
    var2 = "Textfield" + i;
    JQuery('#' + ff_getElementByName(var2).id).css('text-align', 'right');
}

Try this:

$('[name^="Textfield"]').css('text-align', 'right');

This uses jQuery's Attribute starts with selector

Use the below code:

for (var i=1; i<5; i++)
{
var textfields = "Textfield"+i;
var ids = jQuery("[name="+textfields+"]").attr("id");
JQuery('#'+ids).css('text-align', 'right');
}

本文标签: javascriptAlign textfields right via jQueryCSS and within a for loopStack Overflow