2011-09-01 61 views
0

我有5个下拉列表和一个提交按钮。下拉列表中的每个内容都取决于上一个下拉列表的选择值。我想确保即使点击提交按钮后,每个下拉列表的选定值仍保持选中状态。任何人都可以帮助我吗? 我看到一个网站,这是否但是在ASP.NET制成,我更喜欢TU使用PHP,JavaScript的如何使动态下拉列表中的选定值保持选定状态?

<script type="text/javascript"> 
function changeSelect(lang, num, valueElement, nameSelect, nameSpan, dataSup, endId){ 
    var boxName = document.getElementById(nameSelect); 
    var hackIeSpan = document.getElementById(nameSpan); 
    var contentOption = $.ajax({ 
      url: "select.php", 
      global: false, 
      type: "POST", 
      data: { 
       lang : lang, 
       num : num, 
       select : valueElement, 
       dataSup : dataSup, 
       endId : endId 
      }, 
      dataType: "html", 
      async:false, 
      success: function(msg){ 
     } 
     } 
    ).responseText; 
    hackIeSpan.innerHTML = contentOption; 
} 

<form id="searchForm" method="post" action="search.php?search&page=0&lang=<?php echo $lang; ?>"> 
      <fieldset> 

       <select name="sort" onchange="changeSelect('<?php echo $lang; ?>', 1, this.value, 'type_list', 'hackIeType', 'null', 'null'); return true;"> 
        <option value="0"><?php echo trad('SEARCH_01', $lang); ?></option> 
        <option value="Rental"><?php echo trad('SEARCH_01a', $lang); ?></option> 
        <option value="Sale"><?php echo trad('SEARCH_01b', $lang); ?></option> 
       </select> 

       <span id="hackIeType"> 
        <select name="type_list"> 
         <option value="0"><?php echo trad('SEARCH_04', $lang); ?> ...</option> 
        </select> 
       </span> 

.......

<span id="hackIeSleeps"> 
        <select name="bedrooms"> 
         <option value="0"><?php echo trad('SEARCH_05', $lang); ?> ...</option> 
        </select> 
       </span> 
       <span id="spanEndId"> 
        <input id="endId" name="endId" type="hidden" value="" /> 
       </span> 

       <input id="submit_search_home" type="submit" value="<?php echo trad('BTNSEARCH', $lang); ?>" /> 

      </fieldset> 

回答

1

当您按下提交按钮时会发生什么?你最终回到同一页面吗?您只需根据$_POST中发送的值给出所需的选项selected="selected"属性。

为例(当您按下提交这可能不为你工作,这取决于究竟发生了什么):

<form id="searchForm" method="post" action="search.php?search&page=0&lang=<?php echo $lang; ?>"> 
    <fieldset> 
    <select name="sort" onchange="changeSelect('<?php echo $lang; ?>', 1, this.value, 'type_list', 'hackIeType', 'null', 'null'); return true;"> 
     <option value="0"<?php if ($_POST['name'] === '0') echo ' selected="selected"'; ?>><?php echo trad('SEARCH_01', $lang); ?></option> 
     <option value="Rental"<?php if ($_POST['name'] == 'Rental') echo ' selected="selected"'; ?>><?php echo trad('SEARCH_01a', $lang); ?></option> 
     <option value="Sale"<?php if ($_POST['name'] == 'Sale') echo ' selected="selected"'; ?>><?php echo trad('SEARCH_01b', $lang); ?></option> 
    </select> 
    <span id="hackIeType"> 
<?php 

    if ($_POST['name'] === '0') { 
    // The first select value is default, so we can just output a standard select 
    // to be populated by AJAX 

?> 
     <select name="type_list"> 
     <option value="0"><?php echo trad('SEARCH_04', $lang); ?> ...</option> 
     </select> 
<?php 

    } else { 
    // The first select is populated, so copy the logic that generates your second 
    // select from 'select.php' and put it here, then output it 
    } 

?> 
    </span> 

.......

<span id="hackIeSleeps"> 
<?php 

    if ($_POST['type_list'] === '0') { 
    // The second select value is default, so we can just output a standard select 
    // to be populated by AJAX 

?> 
     <select name="bedrooms"> 
     <option value="0"><?php echo trad('SEARCH_05', $lang); ?> ...</option> 
     </select> 
<?php 

    } else { 
    // The second select is populated, so copy the logic that generates your third 
    // select from 'select.php' and put it here, then output it 
    } 

?> 
    </span> 
    <span id="spanEndId"> 
     <input id="endId" name="endId" type="hidden" value="" /> 
    </span> 
    <input id="submit_search_home" type="submit" value="<?php echo trad('BTNSEARCH', $lang); ?>" /> 
    </fieldset> 
+0

当我按下提交按钮,他把我发送到search.php?search&page = 0&lang = uk,但你可以看到在action =“search.php?search&page = 0&lang = <?php echo $ lang;?>”。 – Vladucu

+0

@ user921130是的,但是它与您发布代码的页面相同吗?换句话说,你发布的HTML代码是'search.php'的一部分还是其他页面的一部分? – DaveRandom

+0

选择=“选定”属性只适用于第一部分,当您选择租赁或销售,第二个选择没有选择,因为您需要用鼠标选择(onchange =“changeSelect(...)”)第一个选择工作第二个。看到第一个JavaScript代码,如果你需要我把一些代码frome select.php – Vladucu

相关问题