选择元素中的重定向问题 [英] Redirect issue in select element

查看:43
本文介绍了选择元素中的重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了依赖项下拉列表.

I have used dependency drop downs.

 <html>
 <body>
  <select id="parent-cat" name="parent-cat" onchange="getChildrenList(this.value)">    
            <option value ="">- Please Select -</option>               
                <option value="1">test1</option> 
                <option value="2">test2</option> 
                <option value="3">test2</option>                        
        </select>

     <select id="child-cat" name="child-cat" 
        onchange="getChildUrl(this.value)">    
            <option value ="">- Product category -</option>
        </select>

    <script>    
function getChildrenList(parentId) {
    if(parentId !=''){
        var customurl = "result.php";
        jQuery.ajax({
             url: customurl,
             type: "POST",
             data : 'category_id='+parentId,
             dataType: 'json',
             success : function(result) {                
                var optionHtml; 
                optionHtml +='<option value="">-Product Category-</option>';                
                for(var option of result){
                  optionHtml +='<option value="'+option.url+'">'+option.name+'</option>';
                }
                jQuery('#child-cat').html(optionHtml);
             }
        }); 
   }
}

function getChildUrl(childUrl) {
    var url = childUrl; 
      if (url) { 
          window.location = url; // redirect
      }
      return false;
 }
</script>

  </body>
 </html>

一旦第一个下拉列表更改,我会将结果附加到下一个下拉列表中.

I am appending the result into next drop down once first drop down is changed.

我的问题是,一旦选择了第二个下拉菜单,它就会重定向到 url,但未选择该选项.

My issue is Once the second drop down is selected it is redirecting to the url, but the option is not selected.

我正在寻找在更改第二个下拉列表后重定向 url 后如何选择两个下拉列表的代码.

I am looking for code how both the drop downs are selected once the url is redirected after changing the second dropdown.

请任何人帮助我.谢谢

推荐答案

我相信您的问题出在 getChildrenList 函数中.您永远不会检查您的 url 是否与您当前添加的选项相同,并且您永远不会向选项添加 select 参数,因此即使您导航到该 url,它也不会保持选中状态.由于您生成下拉列表的方式(即每次加载页面时都在客户端重新生成它),您还需要在每次打开页面时检查是否在客户端选择了该选项.请注意,我尚未验证以下解决方案是否有效.

I believe your problem lies within the getChildrenList function. You are never checking to see if your url is the same as the option you are currently adding and you are never adding the select parameter to the option hence it is not staying selected even though you have navigated to the url. Due to the way you are generating your drop-down list (i.e. you are regenerating it client side every time the page is loaded) you will also need to check if the option is selected client-side each time the page is opened. Note I have not verified if the following solution works.

function getChildrenList(parentId) {
    if(parentId !=''){
        var customurl = "result.php";
        jQuery.ajax({
             url: customurl,
             type: "POST",
             data : 'category_id='+parentId,
             dataType: 'json',
             success : function(result) {
                var optionHtml; 
                optionHtml +='<option value="">-Product Category-</option>';                
                for(var option of result){
                  var selected = (window.location == option.url) ? "selected=\"selected\"" : ""
                  optionHtml +='<option value="'+option.url+'" '+selected+'>'+option.name+'</option>';
                }
                jQuery('#child-cat').html(optionHtml);
             }
        }); 
   }
}

这篇关于选择元素中的重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆