Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery如何遍历到<;表格>;从<;按钮>;与后代之间_Jquery_Forms_Button_Traversal_Descendant - Fatal编程技术网

JQuery如何遍历到<;表格>;从<;按钮>;与后代之间

JQuery如何遍历到<;表格>;从<;按钮>;与后代之间,jquery,forms,button,traversal,descendant,Jquery,Forms,Button,Traversal,Descendant,我想知道,在JQuery中,如何在不使用id的情况下从选择器$('button')获取到元素的值? 我知道如何使用id来获取它,但不知何故,我需要上面的方法 我尝试了alert($('button').parent().prev().get(0.nodeName)it弹出FORM,这意味着我到了那里。但是当我使用$('button').parent().prev().get(0).submit()或.serialize()甚至.attr('name')时,它就不起作用了 结构如下,我有10行……

我想知道,在JQuery中,如何在不使用id的情况下从选择器
$('button')
获取到
元素的值? 我知道如何使用id来获取它,但不知何故,我需要上面的方法

我尝试了
alert($('button').parent().prev().get(0.nodeName)
it弹出
FORM
,这意味着我到了那里。但是当我使用
$('button').parent().prev().get(0).submit()
.serialize()
甚至
.attr('name')
时,它就不起作用了

结构如下,我有10行
……


去
您有以下HTML:

<table>
<tr><form name='form' method='POST' autocomplete='on'>
<td id='df'><button type='button'>Go</button></td>
.......
</tr>
</table>
然后,您可以以
$(…).closest('tr').find('form:first')
的形式访问该表单,但该表单无论如何都不会包含其预期的输入,因此请小心

您需要更改HTML(如果只有一行,只需将表单环绕在表中)。如果要坚持使用表,可能还需要更改javascript。这是一个想法(未经完善、未经测试):


将您的html更改为此,然后重试

<form name='form' method='POST' autocomplete='on'>
<table>
    <tr>
       <td id='df'><button type='button'>Go</button></td>
       <td><input type='reset' value='Clear'/></td>
       <td><input name='date'</td>
       <td><input name='customer'</td>
    .......
   </tr>
</table>
</form>

我想这是因为
.get()
方法返回一个节点,所以不能对其调用jQuery方法,比如
.submit()
.attr()

由于您只获得一个元素,我认为在提交或获取name属性之前不需要使用
.get()
方法


$('button').parent().prev().attr('name')
实际上给了我你的表单的名称。

$(…).closest('form')
,但我认为你的HTML是无效的:你不能将
表单
tr
td
之间分割,所以它可以归结为DOM的实际内容。^JanDvorak说什么;你的HTML肯定是无效的。将
声明移动到
之前,并在表格之后关闭它。或者,如果每行需要一个表单,则必须重新考虑布局;您可能无法有效地使用
(您可能需要多个表,或者只基于
的方法。是的,表单必须在表外,否则它将不起作用。必须在表外,甚至之前都不起作用?是的,你们是对的。所以我必须使用。谢谢大家!这是一个学习的好地方!恐怕OP每行需要/想要一个表单。而不是尝试将表用在它提供的另一个表中。)“未定义”,所以我猜.serialize()不能正常工作?
<table>
  <tr>
    <form>
      <td>
        <button/>
      </td>
    </form>
  </tr>
</table>
<table>
  <tr>
    <form/>
    <td>
      <button/>
    </td>
  </tr>
</table>
$('table button').click(function(){
  $(this)
    .closest('table')
    .find('tr')
    .not($(this).closest('tr'))
    .find('input')
    .attr('disabled', 'disabled')
  ;
});
<form name='form' method='POST' autocomplete='on'>
<table>
    <tr>
       <td id='df'><button type='button'>Go</button></td>
       <td><input type='reset' value='Clear'/></td>
       <td><input name='date'</td>
       <td><input name='customer'</td>
    .......
   </tr>
</table>
</form>