javascript - How to reference an input within a form in jQuery - Stack Overflow

An easy jQuery question.I have several identical forms ( except their name ) on one page with a few hid

An easy jQuery question.

I have several identical forms ( except their name ) on one page with a few hidden inputs in each. I want to refer to them by using the form name and then the input name. ( the input names are not unique in my page )

So for instance:

var xAmt = $('#xForm'+num).('#xAmt');

I really want to supply these values to an AJAX POST

    $.ajax({
      url: "X.asp",
      cache: false,
      type:  "POST",
      data:  "XID=xID&xNumber=xNum&xAmt=xAmt",

...

If I can get the values in the AJAX call even better.

An easy jQuery question.

I have several identical forms ( except their name ) on one page with a few hidden inputs in each. I want to refer to them by using the form name and then the input name. ( the input names are not unique in my page )

So for instance:

var xAmt = $('#xForm'+num).('#xAmt');

I really want to supply these values to an AJAX POST

    $.ajax({
      url: "X.asp",
      cache: false,
      type:  "POST",
      data:  "XID=xID&xNumber=xNum&xAmt=xAmt",

...

If I can get the values in the AJAX call even better.

Share Improve this question edited Oct 30, 2008 at 20:27 Codebeef 44.1k23 gold badges89 silver badges119 bronze badges asked Oct 30, 2008 at 19:38 Brian GBrian G 55.1k58 gold badges127 silver badges141 bronze badges 3
  • Why spend money on something you can get for free on the jQuery web site? :-) – Tomalak Commented Oct 30, 2008 at 19:47
  • Not finding the information that helpful. Is the official documentation good or are you talking about the examples/tutorials – Brian G Commented Oct 30, 2008 at 19:51
  • I mean the official docs. I've gotten off the idea of buying books on volatile topics like this one. Also, books lack an important feature: you cannot full text search through them (apart from "with your eyes", that is). – Tomalak Commented Oct 30, 2008 at 19:55
Add a ment  | 

4 Answers 4

Reset to default 2
function queryX( args ) {
    var queryString = [ "XID=", args.XID, "&xNumber=", args.xNumber, "&xAmt=", args.xAmt ].join("");
    $.ajax({
        url: "X.asp",
        cache: false,
        type:  "POST",
        data:  queryString,
        success : function( data ) {
            return data;
        }
    });
}
var myReturnData = queryX({
    XID : $("input[name='XID']").val(),
    xNumber : $("input[name='xNumber']").val(),
    xAmt : $("input[name='xAmt']").val()
});

EDIT:

This allows you the most flexibility, and if only the input values will change (but the query string variables won't), then you can pass in whatever you want as the value.

Looks like you want formSerialize() (or even ajaxSubmit()) from the jQuery forms plugin.

The flexible way to do it has already been answered here, but you can also just make it work with your current code. (Forgive me if this was too basic for what you're looking for.)

Drill down from the unique form name by using the CSS descendant selector (a space):

var xAmt = $('#xForm'+num+ ' #xAmt').val();

Repeat for each value you need and call $.ajax just like you're doing.

Edited:

You need to start with getting them all selected, then you can work with them.

To access just the xForm# elements, you can do a selector like this (you should check the syntax, I haven't run this, it's just an example):

$('form input[name*='xAmt']  ').each(function(){ alert( $(this).val() ); alert( $(this).parent().attr('name') + $(this).parent().attr('id') ); });

The form input[name*='xAmt'] means select any elements inside a form that contain the term xAmt. You can also do starts with, ends with, etc. See the links below.

When in the each loop, you can access each one as $(this). You can access it's parent form as $(this).parent(), so you can tell which form the input is in.

You might build the data in the loop, or do something else with them, and then make the post.

see:

http://docs.jquery./Selectors/attributeContains#attributevalue

http://docs.jquery./Selectors

http://docs.jquery./Each

http://docs.jquery./Attributes/attr#name

http://docs.jquery./Traversing/parent#expr

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744336878a4569166.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信