HTML输入必需属性不起作用

时间:2013-07-10 11:34:06

标签: php jquery html5

如上所述,require属性对我不起作用,我已经搜索了一下但是我在这里和网上其他地方找到的答案无法帮助我,如果有人可以帮助我会很棒

应用程序没什么特别的,对我来说只需要学习php和jquery,这里是:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Sig Generator</title> 
    <script type="text/javascript" src ="jquery-2.0.3.min.js"></script>
    <script type="text/javascript" src="sig.js"></script>
</head>
<body>  
    <div id="legalform" align ="center">

            Rechtsform auswählen:<br><br>
            <select id="selection" size="1">        
            </select><br><br>

            <input id="sendLegalForm" type="button" value ="Weiter"/>           
    </div>  

    <div id="fields" align="center">
            <form id="fieldsForm" > 
            <br>
            <br>    
            <input id="sendFields" type="submit" value="Weiter"/>   
            </form>

            <br>

    </div> 

    <div id="display" >


        <textarea  id="textarea" cols="30" rows="20"></textarea>

    </div>

</body>
</html>

jquery

$(document).ready(function() {
    var $selection ="";

     $.ajax({
         type:'GET',
         url:'http://localhost/php/sig.php?legalforms=true',
         dataType:'json',
         success: function (data){
            $("#display").hide();       
            var selection = [];
            for(var i = 0; i<data.length; i++){
                selection.push('<option>', data[i],'</option>');
            }
            $("#selection").html(selection.join(''));            
            $("#fields").hide();
         },
         error: function(jqXHR,textStatus,errorThrown){  
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
         }
     });

     $("#sendLegalForm").click(function () {    
         selection = $('#selection').val();
         $.ajax({
             type:'GET',
             url:'http://localhost/php/sig.php?selectedLegalform='+ selection,
             dataType:'json',
             success: function (data){  

                 $("#legalform").hide();
                 $("#fields").show();
                 var fieldnames =[];
                 for(property in data.namesToSubmit){
                    fieldnames.push(property);
                 }                   
                 var fields=[];
                 for(var i=0; i<data.textfieldHeaders.length; i++){               
                 fields.push(data.textfieldHeaders[i],'<br>','<input name="',fieldnames[i],'" type="text"',data.namesToSubmit[fieldnames[i]] == "required"?"required":"",'>','<br/>');                                                               
                 }                

                 fields.push("<br>", 'Pflichtfelder (*)');
                 $("#fieldsForm").prepend(fields.join(''));              
             },
             error: function(jqXHR,textStatus,errorThrown){
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
             }
         });
     });

      $("#sendFields").click(function(){     
         $.ajax({
             type:'POST',
             url: 'http://localhost/php/sig.php?fieldsend='+selection,
             data: $("#fieldsForm").serialize(),
             success: function (data){                  
                 $('#fields').hide();
                 $('#display').show();
                 $("#textarea").html(data);
             },
             error: function(jqXHR,textStatus,errorThrown){
                alert('Bitte alle Pflichtfelder ausfüllen.');
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
             }  
        });
        return false;
     });

});

和php文件

<?php

header('Content-type: application/json;  charset=UTF8');
header('Access-Control-Allow-Origin: *');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: 0");   

if (isset($_GET['legalforms'])) {       
    $legalform = array( 'Aktiengesellschaft',
                        'GmbH');

    echo json_encode($legalform);   
}   else if(isset($_GET['selectedLegalform'])) {
        $selection = $_GET['selectedLegalform'];

        $fieldArray = array();

        if ($selection == 'GmbH'){

                $fieldArray['namesToSubmit'] = array(   'companyName',                                                      
                                                        'location',
                                                        'phone',
                                                        'fax',
                                                        'web',
                                                        'registryCourt',
                                                        'registryNumber',
                                                        'ceo'); 

                $fieldArray['textfieldHeaders'] = array('Firmenname und Rechtsform*', 
                                                        'Anschrift*',
                                                        'Telefon',
                                                        'Fax',
                                                        'Webseite',
                                                        'Registergericht*',
                                                        'Registernummer*', 
                                                        'Geschäftsführer*');

        }

        if($selection =='Aktiengesellschaft'){

                $fieldArray['namesToSubmit'] = array(   'companyName'=>'required', 
                                                        'location' =>'required',
                                                        'phone'=>null,
                                                        'fax'=>null,
                                                        'web' => null,
                                                        'registryCourt'=>'required',
                                                        'registryNumber'=>'required',
                                                        'board'=>'required',
                                                        'chairmanboard'=>'required');

                $fieldArray['textfieldHeaders'] = array('Firmenname und Rechtsform*',                                                                                                       'Anschrift*',
                                                        'Telefon',
                                                        'Fax',
                                                        'Webseite',
                                                        'Registergericht*',
                                                        'Registernummer*',
                                                        'Vorstand*',
                                                        'Aufsichtsratsvorstzender*');   
        }   
        echo json_encode($fieldArray);

    } else if ($_GET['fieldsend']) {
        $selection = $_GET['fieldsend'];        

        $signatur = "--\n"; 
        foreach($_POST as $key => $value) {
            if($selection == 'Aktiengesellschaft'){
            // do something..
            }       
        }       
        echo json_encode($signatur);
    }

?>

1 个答案:

答案 0 :(得分:3)

您正在收听按钮的click事件,而不是表单的submit事件。因此,不会触发任何表单验证[请参阅fiddle]。