Javascript 选择文本框时焦点不工作
我这里有一个表单,我希望用户能够选择单选按钮来选择预定义的金额选项,或者按文本框焦点来选择自定义金额。下面javascript的目的是在选择预定义的数量时清除文本框。它还允许用户单击Focus上的文本框,在CP_otheramount字段中输入自定义金额,也可以使用单选按钮作为备用 除了onfocus之外,这一切似乎都在起作用。它在加载时工作得非常好…但请尝试以下场景: 加载页面,在文本框内单击,然后选择值3单选按钮64…决定改变主意,然后再次在onfocus文本框内按。。。它将被禁用,并阻止您单击内部或在中写入数字 有人能看出这里的问题吗?这很奇怪,因为它在Stackoverflow上工作,而不是在live站点上。任何帮助都将不胜感激 以下是迄今为止创建的内容: 功能激活{ $“其他”。属性“已检查”,为真; $“其他”。触发“单击”; $‘theamount’。焦点; } $'input[name=am_payment]'。单击,函数{ 如果$this.val=={ $'input[name=CP_otheramount]'.val; $'theamount'。删除已禁用的; }否则{ $'theamount'。已禁用,已禁用; $'input[name=CP_otheramount]'.val; } }; 64 100 250 另外Javascript 选择文本框时焦点不工作,javascript,jquery,textbox,onfocus,Javascript,Jquery,Textbox,Onfocus,我这里有一个表单,我希望用户能够选择单选按钮来选择预定义的金额选项,或者按文本框焦点来选择自定义金额。下面javascript的目的是在选择预定义的数量时清除文本框。它还允许用户单击Focus上的文本框,在CP_otheramount字段中输入自定义金额,也可以使用单选按钮作为备用 除了onfocus之外,这一切似乎都在起作用。它在加载时工作得非常好…但请尝试以下场景: 加载页面,在文本框内单击,然后选择值3单选按钮64…决定改变主意,然后再次在onfocus文本框内按。。。它将被禁用,并阻止您
代码的问题是,您正在检查每个单选按钮,而没有取消检查它们
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>
无法在页面加载时单击文本框,因为您添加了一个禁用的属性,因此除非单击其他单选按钮,否则无法启用文本框的可能副本,并且如果禁用文本框,则无法获取焦点。我已完成上述操作,但是,如果您首先为值100、64或250选择一个单选按钮,然后通过单击或按文本框内部转到其他位置,则文本框仍然不允许您在其中单击。