开发者

Jquery select a input filed from a form using wildcard selector

I would like to select a input field from a form on the fly, which mean, I don't know wh开发者_开发百科ich form it would be. consider the following code: ( Suppose the input field id is always 'input1' across all forms)

$('[id^=myform]').submit(function(){
   var formId = $(this).attr('id');
   var result = $('#' + formId + ' input#inputl').val();
   ...
});

I am looking for a better solution for my purpose. Is there any?


You'd be better off using the name attribute of the element (having multiple elements with the same ID is invalid in HTML), and making sure those names are kept consistent across forms:

$('[id^=myform]').submit(function(){
  var result = $(this).find('[name="email"]').val();
  ...
});


$('[id^=myform]').submit(function(){
   var result = $(this).find('#input1').val();
});

Although like commented above. Ids are supposed to be unique across the entire page so you might need to change the id attribute into a name attribute for all of those input fields.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜