我有2個輸入與相同類:jQuery的獲得不是空的輸入值的一個
<input type="text" name="test" class="test">
<input type="text" name="test" class="test" value="test">
一個輸入是空的第二 - 不。我如何獲得不輸入空值?主要規則,輸入是完全相同的,我不能添加一些類或其他東西來定義差異。
我有2個輸入與相同類:jQuery的獲得不是空的輸入值的一個
<input type="text" name="test" class="test">
<input type="text" name="test" class="test" value="test">
一個輸入是空的第二 - 不。我如何獲得不輸入空值?主要規則,輸入是完全相同的,我不能添加一些類或其他東西來定義差異。
您可以使用連詞用.filter()
或Attribute Equals Selector [name=」value」]類選擇
var nonEmptyInputs = $('input.test[name="test"]').filter(function(){
return this.value.length > 0;
});
您可以使用一個循環像
$('button').click(function() {
var value;
$('.test').each(function() {
if (this.value.trim()) {
value = this.value.trim()
return false;
}
});
snippet.log(value)
})
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="test" class="test">
<input type="text" name="test" class="test" value="test">
<button>test</button>
試試這個
$(function(){
$('button').click(function() {
$.each($('.test'),function(){
if(!$(this).val())
$(this).val('this text field was not filled');
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text" name="test" class="test">
<input type="text" name="test" class="test">
<button>test</button>