在这里查看它的实际情况:http://jsbin.com/relamiqeru/1/
我试图做到这一点,以便如果用户在优惠券代码字段中输入“idh4”,我的“booyah”div 就会填充所提供的 html。
我觉得的问题是,javascript 仅在页面加载时执行,因此即使在字段中输入 idh4,它也不再检查它。也许 if 语句在这种情况下不适合使用?
根据要求,这里是 jsbin 代码:
<div id="booyah"></div>
<form action="" method="POST">
Coupon Code?<input id="couponcode" type="text" name="coupon code" value="idh4"><br>
<input type="submit">
</form>
$("#couponcode").change(function(){
if ($("#couponcode").val() === 'idh4') {
console.log('it got called');
$( "#booyah" ).html("<p>That code gets you free snappie stickers!</p>");
}
});
最佳答案
我不确定这是否是您想要的,但您无论如何都可以看一下:
$(document).ready(function(){
$("#booyahInput").on('keyup', function(){
if ($(this).val() == 'idh4') {
console.log('it got called');
$( "#booyah" ).html("<p>That code gets you free snappie stickers!</p>");
}
});
});
关于javascript - 页面加载后如何执行if语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28776258/