gpt4 book ai didi

javascript - 根据其他元素的值隐藏元素

转载 作者:行者123 更新时间:2023-11-29 18:05:43 26 4
gpt4 key购买 nike

我试图根据两个字段的值隐藏一个表,这样如果 field2 等于 field1 表就会被隐藏。

JSfiddle

HTML:

<form>
Expected Number of Items: <input type="text" value="14" name="totalItems" id="totalItems">
<p>
Number of Items Entered: <input type="text" value="14" name="enteredItems" id="enteredItems">
</form>
<p>
<table border="1" style="width:100%" id="hideThis">
<tr>
<td>This should be hidden when "totalItems" equals "enteredItems"</td>
</tr>
</table>

JS:

function toggleClass(eid, myclass){
var theEle = document.getElementById(eid);
var eClass = theEle.className;

if(eClass.indexOf(myclass) >= 0){
theEle.className = eClass.replace(myclass, "");
}else{
theEle.className += "" +myclass;
}
}

最佳答案

查看代码中的注释。

// Function to hide/show the table based on the values of inputs
function toggleTable() {

// Hides the table if the values of both input are same
$('#hideThis').toggle($('#totalItems').val() !== $('#enteredItems').val());
}

$(document).ready(function() {
// Bind the keyup event on both the inputs, call the function on event
$('#totalItems, #enteredItems').on('keyup', toggleTable).trigger('keyup');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form>Expected Number of Items:
<input type="text" value="14" name="totalItems" id="totalItems">
<p>Number of Items Entered:
<input type="text" value="14" name="enteredItems" id="enteredItems">
</form>
<p>
<table border="1" style="width:100%" id="hideThis">
<tr>
<td>This should be hidden when "totalItems" equals "enteredItems"</td>
</tr>
</table>

jsfiddle Demo

关于javascript - 根据其他元素的值隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com