Javascript 填写特定输入后如何显示div
此项工作:Javascript 填写特定输入后如何显示div,javascript,jquery,html,Javascript,Jquery,Html,此项工作: 我想知道如何改变这一点,使它能够处理表单的特定部分,而不是全部 我已经设法让它只使用ID,而不使用类 到目前为止,我已经做到了: $('.weeks').bind('keyup change',function(){ // get elements that are empty. var empty = $('.weeks').map(function(index, el) { r
我想知道如何改变这一点,使它能够处理表单的特定部分,而不是全部 我已经设法让它只使用ID,而不使用类 到目前为止,我已经做到了:
$('.weeks').bind('keyup change',function(){
// get elements that are empty.
var empty = $('.weeks').map(function(index, el) {
return !$(el).val().length ? el : null;
}).get();
// could also be placed outside of the function
var tick = $('#tick3');
var cross = $('#cross3');
// check if there are any empty elements, if there are none, show numbers, else hide number.
!empty.length ? tick.show() : tick.hide();
!empty.length ? cross.hide() : cross.show();
});
这是html:
<span class="checked tick" id="tick3"><img src="http://www.clker.com/cliparts/9/d/J/p/D/g/check-grey-md.png" width="38px" style="margin-top: -10px;" /></span>
<span class="checked" id="cross3"><img src="http://mypad.northampton.ac.uk/12402247/files/2014/02/red-wrong-cross-hi-23y1kex.png" width="38px" style="margin-top: -10px;" /></span>
<table>
<tr class="bottom">
<td>
<input type="checkbox" id="allWeeks" class="weeks" name="all" onClick="allWeeks(this)"></input>
<label for="allWeeks">1-15</label>
</td>
<td>
<input type="checkbox" id="defWeeks" class="weeks" checked name="default" onClick="defaultWeeks(this)"></input>
<label for="defWeeks">1-12</label>
</td>
<td>
<input type="checkbox" id="oddWeeks" class="weeks" name="odd" onClick="oddWeeks(this)"></input>
<label for="oddWeeks">Odd</label>
</td>
<td>
<input type="checkbox" id="evenWeeks" class="weeks" name="even" onClick="evenWeeks(this)"></input>
<label for="evenWeeks">Even</label>
</td>
<td colspan="2">
<button id="clearWeeks">Clear</button>
</td>
</tr>
<tr class="bottom">
<td>
<input type="checkbox" id="1" class="weeks all_weeks default_weeks odd_weeks" checked name="1" onClick="changed_check();"></input>
<label for="1">1</label>
</td>
<td>
<input type="checkbox" id="2" class="weeks all_weeks default_weeks even_weeks" checked name="2" onClick="changed_check();"></input>
<label for="2">2</label>
</td>
<td>
<input type="checkbox" id="3" class="weeks all_weeks default_weeks odd_weeks" checked name="3" onClick="changed_check();"></input>
<label for="3">3</label>
</td>
<td>
<input type="checkbox" id="4" class="weeks all_weeks default_weeks even_weeks" checked name="4" onClick="changed_check();"></input>
<label for="4">4</label>
</td>
<td>
<input type="checkbox" id="5" class="weeks all_weeks default_weeks odd_weeks" checked name="5" onClick="changed_check();"></input>
<label for="5">5</label>
</td>
<td>
<input type="checkbox" id="6" class="weeks all_weeks default_weeks even_weeks" checked name="6" onClick="changed_check();"></input>
<label for="6">6</label>
</td>
</tr>
<tr class="bottom">
<td>
<input type="checkbox" id="7" class="weeks all_weeks default_weeks odd_weeks" checked name="7" onClick="changed_check();"></input>
<label for="7">7</label>
</td>
<td>
<input type="checkbox" id="8" class="weeks all_weeks default_weeks even_weeks" checked name="8" onClick="changed_check();"></input>
<label for="8">8</label>
</td>
<td>
<input type="checkbox" id="9" class="weeks all_weeks default_weeks odd_weeks" checked name="9" onClick="changed_check();"></input>
<label for="9">9</label>
</td>
<td>
<input type="checkbox" id="10" class="weeks all_weeks default_weeks even_weeks" checked name="10" onClick="changed_check();"></input>
<label for="10">10</label>
</td>
<td>
<input type="checkbox" id="11" class="weeks all_weeks default_weeks odd_weeks" checked name="11" onClick="changed_check();"></input>
<label for="11">11</label>
</td>
<td>
<input type="checkbox" id="12" class="weeks all_weeks default_weeks even_weeks" checked name="12" onClick="changed_check();"></input>
<label for="12">12</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="13" class="weeks all_weeks odd_weeks" name="13" onClick="changed_check();"></input>
<label for="13">13</label>
</td>
<td>
<input type="checkbox" id="14" class="weeks all_weeks even_weeks" name="14" onClick="changed_check();"></input>
<label for="14">14</label>
</td>
<td colspan="4">
<input type="checkbox" id="15" class="weeks all_weeks odd_weeks" name="15" onClick="changed_check();"></input>
<label for="15">15</label>
</td>
</tr>
</table>
谢谢你的帮助
我不能说您试图做的事情是显而易见的,但可以肯定的是,对checkbox元素调用.val()不会得到您想要的结果。下面是一种很好的获取空元素数组的方法(如未选中的元素):
var empty = $('.weeks').filter(':not(:checked)');
随后,您明确表示希望知道是否选中了任何框,如果是,则显示复选标记;否则显示“X”。因此,您应该得到一个选中元素的数组,而不是空元素:
var checked = $('.weeks').filter(':checked');
然后相应地显示/隐藏图像:
checked.length ? tick.show() : tick.hide();
checked.length ? cross.hide() : cross.show();
您可以使用
$(this)
@Alon引用触发事件的目标对象,我将如何实现它,因为如果至少选中了一个复选框,我需要它用勾号替换十字,如果没有勾号,则再次用勾号替换十字,这是有效的,但只有当我删除上面添加的另一段代码时。如果我留下我以前需要的代码,它就不起作用了。在每个单击处理程序函数的末尾添加以下内容:$('.weeks:first').trigger('change');
checked.length ? tick.show() : tick.hide();
checked.length ? cross.hide() : cross.show();