[英]Is this possible in JQUERY? val() and text()
Is this possible in one liner code in JQUERY? 在JQUERY的一个内联代码中是否有可能?
<table>
<tr>
<!-- Ex. this is the td:nth-child(10) -->
<td>
<input type="hidden" value=""/>
<p> </p>
</td>
</tr>
</table>
// I'd like to ouput a same value in td 10th cell in one liner code..
$(this).children('td:nth-child(10)').text(data[0]).val(date[0]);
Any Help Thanks? 任何帮助谢谢?
OLD ANSWER: Yes, you can. 老答案:是的,可以。 But it won't make any sense since
td
doesn't use val . 但这没有任何意义,因为
td
不使用val 。
The .val() method is primarily used to get the values of form elements such as input, select and textarea.
.val()方法主要用于获取表单元素的值,例如输入,选择和文本区域。 When the first element in the collection is a select-multiple (ie, a select element with the multiple attribute set), it returns an array containing the value of each selected option, or null if no options are selected.
当集合中的第一个元素为select-multiple(即具有设置了multi属性的select元素)时,它将返回一个包含每个选定选项的值的数组;如果未选择任何选项,则返回null。 When called on an empty collection, it returns undefined.
当在空集合上调用时,它返回undefined。
Below is a snippet where text()
is used first, and then val()
. 下面是一个片段,其中首先使用
text()
,然后使用val()
。 But val()
doesnt affect anything. 但是
val()
不会影响任何东西。
Update : With the new HTML, you can set the P
and then the input
. 更新 :使用新的HTML,可以设置
P
,然后设置input
。
changeIt = function(){ $('td:nth-child(2) p').text('foo').closest('td').find('input').val('bar'); };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <input type="hidden" value="inital1"/> <p>1</p> </td> <td> <input type="hidden" value="inital2"/> <p>2</p> </td> <td> <input type="hidden" value="inital3"/> <p>3</p> </td> <td> <input type="hidden" value="inital4"/> <p>4</p> </td> </tr> </table> <button onclick="changeIt()">Click two change 2</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.