到目前为止,如果我在输入字段中输入值“20”,则显示消息“谢谢”。
这是我的 HTML 代码:
<form method="post" action="">
<input type="text" id="nominal" value="">
<input type="submit" value="submit">
</form>
这是我的 JS 代码:
$(document).ready(function(){
var money = 20;
/* not sure if this is written correctly, but this is supposed to
check whether the hidden input element value is equal to var money */
if ($("input[id='nominal']").val() == money ) {
var h = document.createElement("H1") // Create a <h1> element
var t = document.createTextNode("Thank You"); // Create a text node
h.appendChild(t); // Append the text to <h1>
};
});
我创建了一个脚本来满足我的需要,但不起作用!怎么了?
最佳答案
您必须创建一个事件来监听更改,在本例中为changed
。你也可以让你的代码更小一些。 ;)
$(function() {
$("#nominal").change(function() {
if( $(this).val() == 20 )
$(this).after("<h1>Thank You</h1>");
});
});
完整的工作示例,当值再次更改时删除消息并严格检查可以是 seen here .
关于如果我输入值,Javascript 函数会显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38454932/