html结构:
1, <input id="edit-field-14sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text" type="text"></div>
2, <input id="edit-field-15sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text" type="text"></div>
3 , <input id="edit-field-16sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text" type="text"></div>
4 ,<input id="edit-field-17sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text" type="text"></div>
5 ,<input id="edit-field-18sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text" type="text"></div>
现在,我想这样做,如果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();
中的单引号答案 0 :(得分:2)
将此行alert('you can't type them at the same time');
更改为
alert('you can\'t type them at the same time');
答案 1 :(得分:2)
你可以这样做。演示:http://jsfiddle.net/ynhat/x64dH/1/
HTML:
1, <input id="edit-field-14sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text form-text-1" type="text"><br />
2, <input id="edit-field-15sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text form-text-1" type="text"><br />
3, <input id="edit-field-16sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text form-text-1" type="text"><br />
4, <input id="edit-field-17sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text form-text-2" type="text"><br />
5, <input id="edit-field-18sq-und-0-value" name="field_14sq[und][0][value]" value="" size="12" maxlength="10" class="form-text form-text-2" type="text">
使用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');
}
});
})
});
答案 2 :(得分:1)
$("#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');
}
});
等等与其他组合
答案 3 :(得分:0)
你可以使用
jQuery("#form1").keyup(function () {
if(jQuery("#form1").val() != "" && jQuery("#form2").val() != ""){
jQuery("#form3").attr("disabled","disabled");
}
});
并为所有表格重复此操作。