在选择文本框时焦点不起作用
on Focus not working when textbox selected
我这里有一个表单,它像用户能够选择单选按钮来选择预定义的金额选项,或者按文本框(焦点(选择自定义金额。下面的javascript的目的是在选择预定义的金额时清除文本框。它还允许用户单击文本框(对焦(以在CP_otheramount字段中输入自定义金额(也可以使用单选按钮作为回退(。
除了对焦之外,一切似乎都在工作。它在负载上完美运行...但请尝试以下方案:
加载页面,在文本框内单击,然后决定通过选择值 3 单选按钮 (64( 来改变主意...然后决定再次在对焦文本框内按下数量...它被禁用并阻止您单击内部或输入数字!
谁能看到这里的问题?这很奇怪,因为它适用于Stackoverflow,而不是在实时站点上。我的帮助将不胜感激。
以下是到目前为止创建的内容:
function activate() {
$('#other').prop('checked', true);
$('#other').trigger('click');
$('#theamount').focus();
}
$('input[name="am_payment"]').on('click', function() {
if ($(this).val() === '') {
$('input[name="CP_otheramount"]').val('');
$('#theamount').removeAttr("disabled");
} else {
$('#theamount').prop("disabled", "disabled");
$('input[name="CP_otheramount"]').val('');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="radio" name="am_payment" value="3" checked="checked"> <strong>64</strong>
<input type="radio" name="am_payment" value="11" checked="checked"> <strong>100</strong>
<input type="radio" name="am_payment" value="32" checked="checked"> <strong>250</strong>
<input type="radio" value="" name="am_payment" id="other">
<label>Other</label>
<span onclick="activate();"><input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/></span>
代码的问题在于您正在检查每个单选按钮而没有取消选中它们。
function activate() {
// $('#other').prop('checked', true); Remove this line;
$('#other').trigger('click');
$('#theamount').focus();
}
$('input[name="am_payment"]').on('click', function() {
if ($(this).val() === '') {
$('input[name="CP_otheramount"]').val('');
$('#theamount').removeAttr("disabled");
} else {
$('#theamount').prop("disabled", "disabled");
$('input[name="CP_otheramount"]').val('');
}
});
而且
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Only check one, or none by Default -->
<input type="radio" name="am_payment" value="3" checked = "checked"> <strong>64</strong>
<input type="radio" name="am_payment" value="11"> <strong>100</strong>
<input type="radio" name="am_payment" value="32"> <strong>250</strong>
<input type="radio" value="" name="am_payment" id="other">
<label>Other</label>
<span onclick="activate();"><input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/></span>
更新:
它对我有用,但我不知道为什么它不适合你,所以我重写了函数。看看这是否适合您。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="radio" name="am_payment" value="3" checked = "checked"> <strong>64</strong>
<input type="radio" name="am_payment" value="11"> <strong>100</strong>
<input type="radio" name="am_payment" value="32"> <strong>250</strong>
<input type="radio" value="" name="am_payment" id="other">
<label>Other</label>
<span id="toggle"><input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/></span>
<script>
function activate(){
$('#theamount').attr('disabled',false);
$('#theamount').focus();
$('#other').click();
}
function deactivate(){
$('#theamount').val('');
$('#theamount').attr('disabled',true);
}
$('#toggle').click(function(){
activate();
});
$('input[name="am_payment"]').change(function(){
if($(this).val() != ""){
deactivate();
}else{
activate();
}
});
</script>
相关文章:
- 为什么(焦点)没有'在这个角度2的例子中不起作用
- jQuery Opera焦点事件不起作用
- jQuery中的焦点方法没有;不起作用
- 焦点到 DOM 元素不起作用
- 在 ASP 中,键下不起作用以更改焦点
- jQuery 选择的插件 - 激活(即焦点)不起作用
- 当焦点位于文本框中时,单击上的 Jquery 不起作用
- jQuery 焦点事件似乎在 qUnit 测试中不起作用
- Javascript焦点在Firefox中不起作用
- 在选择文本框时焦点不起作用
- jQuery焦点不起作用
- 如果焦点在其他元素上,则keydown事件不起作用
- 切换标签页时,窗口焦点在镶边中不起作用
- Jquery.select()在焦点事件上不起作用
- jQuery焦点在Chrome中不起作用
- 在文本区域内设置焦点不起作用
- HTML 事件焦点不起作用
- 下拉验证,焦点方法不起作用
- Javascript焦点对动态加载的输入框不起作用
- 将焦点()放在下一个表单元素上的Javascript函数不起作用