如何使用jquery来防止其他类型?

html结构:

1, 
2,
3 ,
4 , 5 ,

现在,我想这样做,如果1,2,3,输入框有值,那么就不能在4,5中输入任何内容。

如果4,5输入框有值。 那么不能在1,2,3中输入任何东西。

以下是我的测试代码。 但它无法奏效。

 $(document).ready(function(){ $('#edit-field-14sq-und-0-value').keyup(function() { if ($('#edit-field-16sq-und-0-value').val().length > 0) { alert('you can\'t type them at the same time'); $(this).val(''); $("#edit-field-15sq-und-0-value").val(''); } }) }); 

keyup函数在firebug下给出错误。

编辑:添加“\”以转义alert()中的单引号;

更改此行alert('you can't type them at the same time');

alert('you can\'t type them at the same time');

你可以这样做。 演示: http : //jsfiddle.net/ynhat/x64dH/1/

HTML:

 1, 
2,
3,
4,
5,

使用Javascript:

 $(document).ready(function() { $('.form-text-1').keyup(function() { $('.form-text-2').removeAttr('disabled'); $('.form-text-1').each(function(index){ if ($(this).val() != ''){ $('.form-text-2').attr('disabled','disabled'); } }); }) $('.form-text-2').keyup(function() { $('.form-text-1').removeAttr('disabled'); $('.form-text-2').each(function(index){ if ($(this).val() != ''){ $('.form-text-1').attr('disabled','disabled'); } }); }) }); 
 $("#form1, #form2, #form3, #form4").change(function(){ if ($(this).attr('value')!=''){ $("#form5, #form6, #form7, #form8,#form9, #form10, #form11").attr('readonly','readonly'); } else { $("#form5, #form6, #form7, #form8,#form9, #form10, #form11").removeAttr('readonly'); } }); 

等等与其他组合

你可以用

 jQuery("#form1").keyup(function () { if(jQuery("#form1").val() != "" && jQuery("#form2").val() != ""){ jQuery("#form3").attr("disabled","disabled"); } }); 

并为所有表格重复此操作。

Interesting Posts