如何触发一个依赖于满足特定条件的 2 个表单输入的事件?
我想根据两个表单输入是否匹配来显示/隐藏页面上的元素。因此,在此示例中,如果文本字段大于 100000 并且单选按钮具有特定值,则将显示所述元素。如果仍选择正确的单选按钮,但数值降至 100000 以下,则元素将隐藏。 (此代码是一个更大脚本的一部分,以防它没有意义)。
它在大多数情况下都有效,但与单选值 onclick
一起使用时,文本字段的 onchange
/onkeyup
事件不会自行触发事件。我必须重新单击更改的文本字段值的单选按钮才能触发事件。 (这不是 onblur
问题。onchange
本身可以正常工作,但不能与逻辑 &&
运算符一起使用。)
<li id="f-container-applicant-bondamount">
<label for="f-applicant-bondamount">Amount of Bond</label>
<input type="text" name="ApplicantBondAmount" id="f-applicant-bondamount" onkeypress="showMoreInfo(this);" />
</li>
<li id="f-container-applicant-is">
<label for="f-applicant-is">Applicant is...</label>
<ul>
<li>
<label for="f-applicant-is_0">
<input type="radio" name="ApplicantIs" id="f-applicant-is_0" value="Administrator" onclick="showMoreInfo(this);" />
Administrator
</label>
</li>
<li>
<label for="f-applicant-is_1">
<input type="radio" name="ApplicantIs" id="f-applicant-is_1" value="Executor" onclick="showMoreInfo(this);" />
Executor
</label>
</li>
</ul>
</label>
</li>
<li id="f-container-applicant-moreinfo">
<label for="f-applicant-moreinfo">More Info</label>
<input type="text" name="ApplicantMoreInfo" id="f-applicant-moreinfo" />
</li>
function showMoreInfo(x) {
var bond = document.getElementById('f-applicant-bondamount');
var y = bond.value;
if (y > 100000 && x.value == "Administrator") {
document.getElementById('f-container-applicant-moreinfo').style.display = 'block';
}
else if (y <= 100000 && x.value == "Administrator") {
document.getElementById('f-container-applicant-moreinfo').style.display = 'none';
}
}
谢谢提前。
I want to show/hide elements on the page depending on whether 2 form inputs match. So in this example, if a text field is greater than 100000 and a radio button has a certain value, said elements will show. If correct radio is still selected, but the numerical value drops below 100000, then elements hide. (This code is part of a much larger script, in case it doesn't make sense).
It works for the most part, but the text field's onchange
/onkeyup
event doesn't fire on its own when used with a radio value onclick
event. I have to reclick the radio button for a changed text field value to fire the event. (And it is not an onblur
issue. onchange
works fine on its own, but not with a logical &&
operator.)
<li id="f-container-applicant-bondamount">
<label for="f-applicant-bondamount">Amount of Bond</label>
<input type="text" name="ApplicantBondAmount" id="f-applicant-bondamount" onkeypress="showMoreInfo(this);" />
</li>
<li id="f-container-applicant-is">
<label for="f-applicant-is">Applicant is...</label>
<ul>
<li>
<label for="f-applicant-is_0">
<input type="radio" name="ApplicantIs" id="f-applicant-is_0" value="Administrator" onclick="showMoreInfo(this);" />
Administrator
</label>
</li>
<li>
<label for="f-applicant-is_1">
<input type="radio" name="ApplicantIs" id="f-applicant-is_1" value="Executor" onclick="showMoreInfo(this);" />
Executor
</label>
</li>
</ul>
</label>
</li>
<li id="f-container-applicant-moreinfo">
<label for="f-applicant-moreinfo">More Info</label>
<input type="text" name="ApplicantMoreInfo" id="f-applicant-moreinfo" />
</li>
function showMoreInfo(x) {
var bond = document.getElementById('f-applicant-bondamount');
var y = bond.value;
if (y > 100000 && x.value == "Administrator") {
document.getElementById('f-container-applicant-moreinfo').style.display = 'block';
}
else if (y <= 100000 && x.value == "Administrator") {
document.getElementById('f-container-applicant-moreinfo').style.display = 'none';
}
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:
您的代码不起作用,因为
showMoreInfo(x)
函数假定 x 参数将是对一个或另一个单选按钮的引用,但随后您的文本输入是 onchange/onkeyup/whatever 事件正在调用相同的函数并传递对其自身的引用 - 这就是为什么它仅在您单击单选按钮时才起作用。您可以尝试类似下面的方法,让函数自行计算出对 HTML 元素的引用,而不是将它们传入:不过,我关于键盘事件不充分的原始观点仍然有效。
我原来的答案:
正如 wombleton 所说,
onchange
仅在焦点离开该字段时发生 - 这就是为什么在您的示例中单击单选按钮会使onchange
发生。正如已经说过的,您可以使用onkeyup
而不是onchange
。但是,请记住,用户可以通过多种方式更改输入中的值,而无需使用键盘(因此不需要
onkeyup
),例如,拖放到输入(拖出将改变焦点,但如果您不再使用onchange
...)。或者右键单击-上下文-菜单-粘贴、剪切或删除。我不确定满足这些需求的最简单方法是什么(我不需要担心,因为我不喜欢当用户在字段中键入内容时页面上的内容发生变化,而且我通常只需要 < code>onkeyup 专门检查 Enter 键),但是您可以查看 onpaste 和 ondrop 事件。EDIT:
Your code doesn't work because the
showMoreInfo(x)
function assumes that the x parameter will be a reference to one or the other of the radio buttons, but then your text input's onchange/onkeyup/whatever event is calling the same function and passing in a reference to itself - this is why it only works when you click the radio buttons. You could try something like the following instead, letting the function work out its own references to your HTML elements instead of passing them in:My original point about keyboard events being inadequate still stands though.
My original answer:
As wombleton said,
onchange
only happens when focus leaves the field - which is why in your example clicking on the radio button makes theonchange
happen. So as has been said already you can useonkeyup
instead ofonchange
.But, remember that there are several ways that the user can change the value in an input without using the keyboard (thus no
onkeyup
), e.g., drag-and-drop onto the input (drag out will change the focus, but if you're not usingonchange
any more...). Or right-click-context-menu-paste, cut or delete. Off-hand I'm not sure what the simplest way to cater for these is (I haven't needed to worry since I don't like having things on the page change as the user types within a field and I generally only needonkeyup
to check specifically for the enter key), but there are onpaste and ondrop events you could look into.最可能的答案是文本字段的
onchange
仅在您模糊时触发。单击文本字段之外的任何位置都应该触发该事件。您可以使用
onkeyup
来代替。Most likely answer is that textfield's
onchange
only fires when you blur from it. Clicking anywhere except in the textfield should trigger the event.You can use
onkeyup
instead.onkeypress 或 onkeyup 是侦听文本输入字段更改的正确事件。 onkeypress 在按下或按住时触发,而 onkeyup 在释放按键后触发
onkeypress or onkeyup are the correct events to listen for changes to a text input field. onkeypress fires when pressed or held, while onkeyup fires after a key has been released