如何验证三个字段,以便始终具有值

时间:2014-03-24 12:14:08

标签: jquery

我有以下三个字段:

第一场

<input class="emailrequerido" name="emailrecomendado1_<?php echo $patrocinado->idpatroc; ?>" id="emailrecomendado1_<?php echo $patrocinado->idpatroc; ?>" placeholder="user@domain.com" type="text" />

第二场

<select name="contactos<?php echo $patrocinado->idpatroc; ?>[]" id="contactos<?php echo $patrocinado->idpatroc; ?>" multiple="3" style="width:49%" class="contactorequerido">

第三场

<select name="grupos<?php echo $patrocinado->idpatroc; ?>[]" id="grupos<?php echo $patrocinado->idpatroc; ?>" multiple="3" style="width:49%" class="gruporequerido">

提交字段

<input id="enviarForm" type="submit" class="uk-button uk-float-right" value="<?php echo JText::_('COM_CSTUDOMUS_PATROCINADOR_SEND_RECOM');?>"/>

在您允许提交之前,我需要始终拥有三个字段中的一个字段。

我尝试了以下内容:

 $("#enviarForm").click(function() {
    if($(".emailrequerido").val() == false){
        $('.contactorequerido').attr('required', true);
        $('.gruporequerido').attr('required', true);
    }else if($(".contactorequerido option:selected").val() == false){
        $('.emailrequerido').attr('required',true);
        $('.gruporequerido').attr('required',true);
    }else if($(".gruporequerido option:selected").val() == false){
        $('.emailrequerido').attr('required',true);
        $('.contactorequerido').attr('required',true);
    }

    if($(".emailrequerido").val() == true){
        $('.contactorequerido').removeAttr('required');
        $('.gruporequerido').removeAttr('required');
    }else if($(".contactorequerido option:selected").val() == true){
        $('.emailrequerido').removeAttr('required');
        $('.gruporequerido').removeAttr('required');
    }else if($(".gruporequerido option:selected").val() == true){
        $('.emailrequerido').removeAttr('required');
        $('.contactorequerido').removeAttr('required');
    }
})

4 个答案:

答案 0 :(得分:1)

$(document).ready(function(){
// Disallow submission here initially ...

$(":input").bind("keyup change", function(e) {
var comboVal = $('.emailrequerido').val()+$('.contactorequerido').val()+$('.gruporequerido').val();
    if(comboVal == 'nullnull'){
      // Don't allow!!! May disable or hide submission button
    } else {
      // Allow :D 
    }
});
});

答案 1 :(得分:0)

你可以这样做:

$("#enviarForm").click(function() {
    if($('[id^="emailrecomendado1_"]').val() == "" || 
       $('[id^="contactos"]').val() == "" || 
       $('[id^="grupos"]').val() == ""){
      alert('one field required.');
    }
});

答案 2 :(得分:0)

为他们提供所有相同的课程,例如req。然后做:

$('#enviarForm').submit(function(){
  return $('.req').filter(function(){return $(this).val();}).length > 0;
});

答案 3 :(得分:0)

试试这个 - 可能有所帮助..但不确定..

$("#enviarForm").click(function(e) {
    e.preventDefault();
    var error=0;
    if($(".emailrequerido").val() == false){
        $('.contactorequerido').attr('required', true);
        $('.gruporequerido').attr('required', true);
        error++;
    }
    if($(".contactorequerido option:selected").val() == false){
        $('.emailrequerido').attr('required',true);
        $('.gruporequerido').attr('required',true);
         error++;
    }
    if($(".gruporequerido option:selected").val() == false){
        $('.emailrequerido').attr('required',true);
        $('.contactorequerido').attr('required',true);
          error++;
    }

    if($(".emailrequerido").val() == true){
        $('.contactorequerido').removeAttr('required');
        $('.gruporequerido').removeAttr('required');
    }
    if($(".contactorequerido option:selected").val() == true){
        $('.emailrequerido').removeAttr('required');
        $('.gruporequerido').removeAttr('required');
    }
    if($(".gruporequerido option:selected").val() == true){
        $('.emailrequerido').removeAttr('required');
        $('.contactorequerido').removeAttr('required');
    }
    if(error==0){
        ("#formid").submit();
    }
});