我需要能够知道何时使用val()
以及何时动态使用text()
与jQuery。如何在使用jQuery时动态地使用.val()或.text()?
如果我有一个span
和input
具有相同的类名,我需要在跨应用val()
上的投入和text()
设置它的价值/内容。
这里是我的HTML标记
<input class="att" name="some1" value="" type="text"><br>
<input class="att" name="some2" value="" type="text"><br>
<input class="att" name="some3" value="" type="text"><br>
<div class="att"></div><br>
<div id="test" class="att"></div><br><br>
<div id="cl">Click Me</div>
这里是我试过未设置值如预期
$(function(e){
$('#cl').click(function(e){
//Select any input that has the class "att" and apply the val() function
$('.att :input').val('123');
//Select any element that is not an input and apply the text() function
$('.att').not(':input').text('123');
//Search for an element with a known ID but apply `val()` if the element is input
$('#text .att :input').val('123');
//Search for an element with a known ID but apply `text()` if the element is not an input
$('#text .att').not(':input').text('123');
});
});
我还创建了一个jfiddle我的代码
哪有我正确地应用基于元素类型的val()和文本?
谢谢。它做到了。允许时我会接受你的答案 –