0

我正在构建一个上传表单,用户可以在其中上传文档以及有关文档的详细信息,例如其名称等。我遇到的问题是,当用户使用选择选择类别时,我必须生成另一个选择,然后给出用户选择子类别的选项。请参阅以下内容:-

 $(".department").change(function() {
    var url = "includes/get-categories.php"; // the script where you handle the form input.
    $.ajax({
       type: "POST",
       url: url,
       data: $("#upload-modal").serialize(), // serializes the form's elements.
       success: function(data){
            //this is where the select is generated
            $(".sub").html(data); // show response from the php script.
            //The alert works but the above line does not
            alert("yeahhhhhhhhhhhhhhh boi");
       }
     });

Now this works like it should in that when the select is changed it generates another select from get-categories and gives the user another select box. 但是由于某种原因,当用户提交表单并在表单中显示错误列表(字段留空等)时,将不再通过选择类别来生成选择框。我什至用确实有效的警报测试了代码,所以我真的很困惑为什么以下行不起作用

$(".sub").html(data); 

这是我的表格,非常标准

echo "<form enctype='multipart/form-data' action='".$_SERVER['PHP_SELF']."' method='POST' autocomplete='off' id='upload-modal'>"; 
echo '<fieldset>';
echo '<legend>Please fill in all the fields below:- </legend>';

echo '<label for="docname">Document name</label>';
echo '<input class="block" type="text" name="docname" id="docname" value="'.$_POST['docname'].'" />';

echo '<label for="version">Version (if left blank it will be entered as 1.0)</label>';
echo '<input class="block" type="text" name="version" id="version" value="'.$_POST['version'].'" />';

//We need to now give a drop down for the admin to select a department


    try {
        $conn = new PDO(DB_HOST,DB_USER,DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare('SELECT X FROM Y WHERE Z = :id');
        $stmt->execute(array('id' => A));
        while($row = $stmt->fetch()) {

     // if the user is an admin we provide them with the admin link
            if($row['admin_level']=="super" ||     $row['admin_level']=="admin" && $row['department']=="Assurance" ){
                echo '<select id="department" class="block department" name="department">';
                echo '<option value="0">department...</option>';
                echo '<option value="policies">Policies</option>';
                echo '<option value="procedures">Procedures</option>';
                echo '</select>';                   
            } 

            echo '<p class="sub"></p>';
        }
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }                   



echo '<label for="keywords">Enter keywords (seperate with ",")</label>';
echo '<textarea id="keywords block" name="keywords" rows="4" cols="50" value="'.$_POST['keywords'].'"></textarea> ';

echo '<label for="filename">Supported formats - .docx & .pdf</label>';
echo '<input type="file" name="filename" id="filename" title="Supported formats - .docx and .pdf" />';

echo '<input class="submit-ie6" type="submit" name="submit" value="Upload" id="upload-modal-submit" />';

echo '</fieldset>';
echo '</form>'; 

任何帮助都是最受重视的

4

1 回答 1

0

尝试使用像 Firebug 这样的工具,看看为什么在提交后,这个: $(".sub").html(data); 不起作用。原因可能是因为在您提交表单(有错误)后:

echo '<p class="sub"></p>';

不在这里。

我会建议以下内容:
获取您的

<p class="sub"></p>

没有尝试{},只需使用 css,如:

<p class="sub" style="display:none;"></p>  

因此,您所要做的就是当用户选择一个类别时,创建第二个选择框并从 p 中删除 display:none ,例如:

$(".sub").html(data);  
$(".sub").show();
于 2013-01-23T16:04:48.650 回答