以下两个问题的代码。
我可以补充说我使用的是Ember 1.4(不是选择)
$(document).on('blur', 'input[name="coupon_code"]', function(){
var form = $(this).closest('form');
var couponBtn = form.find('input[name="apply_coupon"]');
var couponCode = form.find('input[name="coupon_code"]');
$(couponBtn).on('click', function(e){
e.preventDefault();
console.log('timmy2')
});
$(couponCode).change(function(){
if($(couponCode).val() == 'coupon10') {
$(couponBtn).removeClass('disabled');
$(couponBtn).unbind('click');
console.log('winning');
} else if ($(couponCode).val() != 'coupon10') {
console.log('hello');
}
});
});
<form name="ApplyCouponForm" method="post" action="/apply_coupon_check">
<div class="form-row">
</div>
<input id="account_id" class="ember-view ember-text-field" type="hidden" name="account_id" value="11">
<div class="form-row with-border">
<div class="field w100">
<label for="coupon_code">If your employer has introduced you to Social CareLine, enter below the coupon code </label>
<input id="ember767" class="ember-view ember-text-field" type="text" name="coupon_code" required="required">
</div>
<input class="apply-coupon disabled button submit-button no-margin-top" type="submit" value="Apply Coupon" name="apply_coupon"> </div>
<div class="clear"></div>
<div class="form-row">
</div></form>
.disabled {
background: #dfdfdf!important;
cursor: default!important;
color: red;
}
.right-column .form-row {
width: 518px; padding: 0px 0px; clear: both;
}
.submit-button {margin-top: 15px;}
.button.submit-button {
padding: 8px 12px 8px 32px;
background: #57d78d url("../images/submit-checkmark.png") no-repeat 10px center;
}
.right-column .main-submenu li.apply-coupon {
background-image: url("../images/submenu-document.png");
}
答案 0 :(得分:1)
您的问题
原因 - 这是因为
change
上input
的{{1}}事件已绑定在同一blur
内,实际上并非bind
}} 除非并且直到它得到focus
然后变得模糊,即lost focus
。但是我不确定为什么should unbind the click event which prevents the default behavior
,因为如果你disabled
向disabled
添加属性button
,它将会在启用它之前不可单击。在下面给出的DEMO
中添加了相同内容。此e.preventDefault()
内button
click event
也unbind
也会这样做,因此恕我直言,无需click event
event
。
原因 - 正如我上面所说,行为是因为元素
disabled
的绑定不当。
<强>解决方案强>
input element
属性为<input class="apply-coupon disabled button submit-button no-margin-top"
type="submit" value="Apply Coupon" disabled name="apply_coupon"/>
^^^^^This here
events
将blur event
移出$('input[name="coupon_code"]').on('input', function() {
var ctrl=$(this);//$(this) refers to current clicked button
var couponBtn=ctrl.closest('form').find('input[name="apply_coupon"]');
//get its respective couponBtn
if (ctrl.val() == 'coupon10') {
$(couponBtn).addClass('disabled').attr('disabled',false);
//remove button disabled attribute
$(couponBtn).removeClass('disabled');
$(couponBtn).unbind('click');//Not sure whether you need this now
console.log('winning');
} else if (ctrl.val() != 'coupon10') {
console.log('hello');
$(couponBtn).addClass('disabled').attr('disabled',true);
//add button disabled attribute
}
})
$('input[name="apply_coupon"]').on('click', function() {
e.preventDefault();
console.log('timmy2')
})
。
event delegation
<强> 强>
FIDDLE DEMO
强>
<强>更新强>
在看完第一条评论后,我觉得这是典型的elements
问题,其中DOM
DOM ready
将在events
之后添加,即动态,但{ {1}}不会附加到他们身上。因此,您需要将events
附加到element
期间出现的DOM ready
以及这些elements
将被追加的位置,或直接附加到document
as你使用了blur event
。在这种情况下,请使用以下代码。
$(document).on('input','input[name="coupon_code"]', function() {
var ctrl=$(this);//$(this) refers to current clicked button
var couponBtn=ctrl.closest('form').find('input[name="apply_coupon"]');
//get its respective couponBtn
if (ctrl.val() == 'coupon10') {
$(couponBtn).addClass('disabled').attr('disabled',false);
//remove button disabled attribute
$(couponBtn).removeClass('disabled');
$(couponBtn).unbind('click');//Not sure whether you need this now
console.log('winning');
} else if (ctrl.val() != 'coupon10') {
console.log('hello');
$(couponBtn).addClass('disabled').attr('disabled',true);
//add button disabled attribute
}
})
$(document).on('click','input[name="apply_coupon"]', function() {
e.preventDefault();
console.log('timmy2')
})