使用单选按钮(jQuery)禁用/启用文本字段

时间:2012-11-05 12:19:32

标签: jquery onclick switch-statement textfield radio

感谢过去的帮助。我再次问你。

我想要2个单选按钮,这将改变文本输入的状态(启用/禁用)。

我设法让它从禁用状态变为启用状态,但问题是,当您在单选按钮之间切换时,文本字段仍为启用状态。我不希望这样。

html是小提琴

$(document).ready( function(){
$('#casodhoda,#casodhoda1').prop('disabled',true);
$('input[type=radio][name=radioknof1]').click( function(){
$('#casodhoda,#casodhoda1').prop('disabled',true).val('');
$(this).next('span').children('wpcf7-form-control-wrap casodhoda').prop('disabled',false).triggerHandler('focus');
});
});
编辑,修改了一点代码,并且无论如何都无法进入工作解决方案..我希望在加载时禁用输入字段,当用户点击无线电时,它应启用两者并专注于第一个输入字段。 http://jsfiddle.net/wLFKD/3/就在这里。谢谢!

3 个答案:

答案 0 :(得分:5)

根据您的网站html完全更改。这应该有效:

$(document).ready( function(){
  $('#casodhoda,#casprihoda').prop('disabled',true);
  $('input[type=radio][name=radioknof]').click( function(){
    $('#casodhoda,#casprihoda').prop('disabled',true).val('');
    $(this).next('span').children('.wpcf7-form-control').prop('disabled',false).triggerHandler('focus');
  });
});

有一个错字。 如果这不起作用,我唯一能想到的另一种方法是......不,我认为这样可行。

答案 1 :(得分:0)

此标记:

<input type="radio" name="radioknof" value="text1"/>
<input type="text" id="id1" disabled="disabled"/><br/><br/>
<input type="radio" name="radioknof" value="text2"/>  
<input type="text" id="id2" disabled="disabled"/>​

和这个脚本:

$('input[name="radioknof"]').on('click', function(){            
    $(this).next().prop('disabled',false).siblings('input[type=text]').prop('disabled',true);
});

会产生您想要的结果:http://jsfiddle.net/uefAK/

答案 2 :(得分:0)

为什么不,不需要Javascript的另一种解决方案

// HTML
<form>
    <div>
        <input id="trigger-1" type="radio" name="radioknof" value="text1"/>
        <label for="trigger-1"><input type="text" id="input-1" /></label>
    </div>
    <div>
        <input id="trigger-2" type="radio" name="radioknof" value="text2"/>
        <label for="trigger-2"><input type="text" id="input-2" /></label>
    </div>
</form>​


// CSS, but just for styling, it doesn't have functional meaning :)
form {padding: 2%}
div {margin: 1em 0}
input[type='radio'] {cursor: pointer;}​

http://jsfiddle.net/nUWcF/

相关问题