我用带有三个输入的附加按钮制作了一个动态表,我想比较一下第一个输入值不大于第二个输入和第三个输入值不大于第一个和第二个..请帮助我解决这个出来...
<table id="myTable" class="form-group" cellpadding="10" table align="center">
<tbody>
<tr>
<td><input type='text' name='from[]' onkeyup='compare();' placeholder='From'id='first'></td>
<td><input type='text' name='to[]' onkeyup='compare();' placeholder='To' id='second'></td>
<td><input type='text' name='gap[]' onkeyup='compare();' placeholder='Gap' id='third'></td>
</tbody>
</table>
<center>
<button type="button" class="btn btn-primary" onclick="myFunction()">+Addmore</button>
<br><br>
<button type="submit" class="btn btn-warning">Submit</button>
</center>
<script type="text/javascript">
function f(e){
document.getElementById('myForm').action=`/create/${e.value}`;
document.getElementById('pro').value=e.options[e.selectedIndex].text;
}
function myFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow();
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = "<input type='text' placeholder='From' name='from[]'>";
cell2.innerHTML = "<input type='text' placeholder='To' name='to[]'>";
cell3.innerHTML = "<input type='text' placeholder='Gap'name='gap[]'>";
}
</script>
<script type="text/javascript">
var first=document.getElementById('first').value;
var second=document.getElementById('second').value;
if(parseInt(first)<parseInt(second))
alert('Second Value is Greater than first');
else if(parseInt(first)>parseInt(second))
alert('First Value is Greater than second');
else if(parseInt(first)==parseInt(second))
alert('First Value and second value is equal');
</script>
答案 0 :(得分:1)
需要将检查代码放入“比较”功能中,并使用正确的比较符号(<>)。这有效:
function compare() {
var first = document.getElementById('first').value;
var second = document.getElementById('second').value;
if(parseInt(first) < parseInt(second))
alert('Second Value is Greater than first');
else if(parseInt(first) > parseInt(second))
alert('First Value is Greater than second');
else if(parseInt(first) == parseInt(second))
alert('First Value and second value is equal');
}
此外,您也可以仅通过其id获得元素(如果没有具有此类名称的变量),因此下一个代码更短,但也可以使用:
function compare() {
if(parseInt(first.value) < parseInt(second.value))
alert('Second Value is Greater than first');
else if(parseInt(first.value) > parseInt(second.value))
alert('First Value is Greater than second');
else if(parseInt(first.value) == parseInt(second.value))
alert('First Value and second value is equal');
}
答案 1 :(得分:1)
但是我也希望当这些条件不成立时,禁用“提交”按钮...当条件为“ true”时,禁用
最好在按钮上添加id="button"
以便很快得到它。这段代码有效:
function compare() {
if(parseInt(first.value) < parseInt(second.value)) {
alert('Second Value is Greater than first');
button.removeAttribute("disabled");
}
else if(parseInt(first.value) > parseInt(second.value)) {
alert('First Value is Greater than second');
button.removeAttribute("disabled");
}
else if(parseInt(first.value) == parseInt(second.value)) {
alert('First Value and second value is equal');
button.removeAttribute("disabled");
}
else button.disabled = "true";
}
我们也可以隐藏它而不是阻止它。