开发者

using Jquery to reference two different forms

I am still new to Jquery and need to reference two forms, the problem is both of my forms have the same IDs. I want to get the name from form1 and populate it into form2, I would problably use this in javascript.

      document.form2.name.value = document.form1.name.value 

but i want to di the same in Jquery something like bel开发者_如何转开发ow. Can anyone help me please and I hope it makes sense to you all , thanks

      ref  = $('#name' FORM1).val() ;

      $('#name' FORM").val( ref ) ; 


It is not valid for two elements to have the same ID. Elements have to have a unique ID in the DOM. What you probably want to do is give them the same name, but different IDs. Then you would do something like:

var formdata = []
$("#form1 input").each(function (index, item) {
   formdata.push($(item).val());
});
$("#form2 input").each(function (index, item) {
   $(item).val(formdata.shift());
});

This assumes that each input in the first form matches the input in the second form in order.


You can reference an element by a specific name like this:

var element = $("[name=" + name + "]");

Where name is a variable holding the name of the element (a form for example)

In your case (without a variable):

var form1 = $("form[name=form1]");
var form2 = $("form[name=form2]");

// copy the value of input element with name "name" from form2 to form1
$("input[name=name]",form1).val($("input[name=name]",form2).val());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜