Php jQuery下拉菜单+;保持选择

Php jQuery下拉菜单+;保持选择,php,jquery,forms,drop-down-menu,selected,Php,Jquery,Forms,Drop Down Menu,Selected,我对JS不是很有经验(PHP是我的强项),所以我需要你的帮助 我有一个脚本,有两个下拉菜单,显示隐藏的内容。我用这个例子 当我执行一个PHP脚本时,它会检查错误,如果有错误,它会将用户返回表单。问题是它不会显示隐藏的内容。我可以设置一些PHP变量,并将它们放入$selected_one='selected=“selected”'这样的输入中,下拉菜单将有正确的选择,但jQuery函数不是这样工作的。我认为它放了一些显示和隐藏内容的类 我不知道发生了什么事,所以我需要你的帮助。有谁能给我一个如何解

我对JS不是很有经验(PHP是我的强项),所以我需要你的帮助

我有一个脚本,有两个下拉菜单,显示隐藏的内容。我用这个例子

当我执行一个PHP脚本时,它会检查错误,如果有错误,它会将用户返回表单。问题是它不会显示隐藏的内容。我可以设置一些PHP变量,并将它们放入$selected_one='selected=“selected”'这样的输入中,下拉菜单将有正确的选择,但jQuery函数不是这样工作的。我认为它放了一些显示和隐藏内容的类

我不知道发生了什么事,所以我需要你的帮助。有谁能给我一个如何解决这个问题的提示吗

这里有一些代码可以使事情可视化

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<style>
.list_publish, .list_news, option_value {
     display: none;   
}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<body>
<?php
$selected_news = '';
$selected_review = '';
$selected_news_text = '';
$selected_news_video = '';

if(isset($_POST['submit_news_text'])){

    $selected_news = 'selected="selected"';
    $selected_news_text = 'selected="selected"';

    $news_title = $_POST['news_title'];

    // Checks blank forms and return error
    if(empty($news_title)) {
        $error_message = 'This field must not be empty!';
        $proceed = false;
    } else {
        $proceed = true;
    }
}

if($proceed){
    // do stuff
    echo 'do stuff';
} else {
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    <select class="change_publish" name="publish_type" id="publish_type" style="width: 238px;">
      <option value="publish_type" data-title="choose">Publishing...</option>
      <option value="publish_news" data-title="news" <?php echo $selected_news ?>>Publish News</option>
      <option value="publish_review" data-title="review" <?php echo $selected_review ?>>Publish Review</option>
    </select>
    <div class="list_publish publish_type"> 
    </div>
    <div class="list_publish publish_news"> 
        <select class="change_news" name="news_type" id="news_type" style="width: 238px;">
          <option value="news_type" selected="selected" data-title="choose">News Type</option>
          <option value="news_text" data-title="text" <?php echo $selected_news_text; ?>>Text</option>
          <option value="news_video" data-title="video"<?php echo $selected_news_video; ?>>Video</option>
        </select>
        <div class="list_news news_type">
        </div>
        <div class="list_news news_text">
            <input name="news_title" type="text" id="news_title">
            <input name="submit_news_text" type="submit" value="Publish" id="submit_news_text" />
        </div>
        <div class="list_news news_video">
            > This function is not available at the moment.
        </div>
    </div>
    <div class="list_publish publish_review">
        > This function is not available at the moment.
    </div>
</form>
<?php
    echo $error_message;
}
?>
<script>
// дропдаун меню
$('.change_publish').change(function(){
    var selected = $(this).find(':selected');
    $('.optionvalue').fadeOut(function(){
         $('.optionvalue').html(selected.html()).fadeIn()
             .attr('class', 'optionvalue '+selected.val());   
    });
    var count = $('.list_publish').length;
    $('.list_publish').slideUp(function(){
        if(!--count) {
           $('.'+selected.val()).slideDown();
        }       
    });
});
$('.change_news').change(function(){
    var selected = $(this).find(':selected');
    $('.optionvalue').fadeOut(function(){
         $('.optionvalue').html(selected.html()).fadeIn()
             .attr('class', 'optionvalue '+selected.val());   
    });
    var count = $('.list_news').length;
    $('.list_news').slideUp(function(){
        if(!--count) {
           $('.'+selected.val()).slideDown();
        }       
    });
});
</script>
</body>
</html>

无标题文件
.list\u publish、.list\u news、option\u value{
显示:无;
}

在文档加载时触发更改事件,如下所示:

<script>
// дропдаун меню
$('.change_publish').change(function(){
    var selected = $(this).find(':selected');
    $('.optionvalue').fadeOut(function(){
         $('.optionvalue').html(selected.html()).fadeIn()
             .attr('class', 'optionvalue '+selected.val());   
    });
    var count = $('.list_publish').length;
    $('.list_publish').slideUp(function(){
        if(!--count) {
           $('.'+selected.val()).slideDown();
        }       
    });
}).change(); // HERE THE CHANGE

$('.change_news').change(function(){
    var selected = $(this).find(':selected');
    $('.optionvalue').fadeOut(function(){
         $('.optionvalue').html(selected.html()).fadeIn()
             .attr('class', 'optionvalue '+selected.val());   
    });
    var count = $('.list_news').length;
    $('.list_news').slideUp(function(){
        if(!--count) {
           $('.'+selected.val()).slideDown();
        }       
    });
}).change(); // HERE THE CHANGE
</script>

// дропдаун меню
$('.change_publish').change(function(){
var selected=$(this.find(':selected');
$('.optionvalue').fadeOut(函数(){
$('.optionvalue').html(selected.html()).fadeIn()
.attr('class','optionvalue'+selected.val());
});
变量计数=$('.list_publish')。长度;
$('.list_publish').slideUp(函数(){
如果(!--count){
$('.+selected.val()).slideDown();
}       
});
}).change();//找零
$('.change_news').change(function(){
var selected=$(this.find(':selected');
$('.optionvalue').fadeOut(函数(){
$('.optionvalue').html(selected.html()).fadeIn()
.attr('class','optionvalue'+selected.val());
});
变量计数=$('.list_news')。长度;
$('.list_news').slideUp(函数(){
如果(!--count){
$('.+selected.val()).slideDown();
}       
});
}).change();//找零

在指定函数后加载文档时,通过在函数后添加
.change()
尝试触发更改事件。这就像是通过编程触发你下拉列表的
change()
事件。我会被诅咒的!我是个十足的白痴,我编辑了我的本地文件,但我仍然在预览演示,我想知道为什么它不起作用。。。非常感谢,这对我帮助很大!