简体   繁体   English

onclick function 在 chrome 中不工作

[英]onclick function is not working in chrome

I am using codeigniter-3 to develop we application,i have one page in that two dropdowns are there both are dependent each other for example if i select first dropdown based on that second dropdown values should populate for this part i have done it's working fine in firefox but not in chrome ,did i miss anything..?我正在使用 codeigniter-3 来开发我们的应用程序,我有一个页面,其中有两个下拉菜单相互依赖,例如如果我 select 第一个下拉菜单基于第二个下拉菜单值应该填充这部分我已经完成它工作正常在firefox但不是在chrome中,我错过了什么..?

<div class="form-group">
    <label for="exampleInputName1">Product Category</label>
    <select name="product_category_lists"  class="form-control" id="cat_id" required="">
     <option value="">--Select Category</option>
     <option value="fresh_food" onclick="home()" >Home</option>
     <option value="broth" onclick="kitchen()">Kitchen</option>
    </select>
</div>
<div class="form-group">
  <label for="exampleInputName1" >Product</label>
  <select name="product_category"  id="cust_drop" class="form-control" required>

    </select>
</div>

<script>
function home(){
    var a = document.getElementById('cust_drop').value;
    document.getElementById('cust_drop').innerHTML ="";
    var options = `
    <option value="">--Select Category</option>
                        <option value="">TV & Appliances</option>
                      `
console.log('options',options);
document.getElementById('cust_drop').innerHTML = options;
}

function kitchen(){
    document.getElementById('cust_drop').innerHTML ="";
    var options = `<option value="">--Select Category</option>`
    document.getElementById('cust_drop').innerHTML = options;
}
</script>

Here you go,Try to use onchange instead of onclick.你这里go,尽量用onchange代替onclick。

<div class="form-group">
    <label for="exampleInputName1">Product Category</label>
    <select onchange="producttypes(this.value)" name="product_category_lists"  class="form-control" id="cat_id" required="">
     <option value="">--Select Category</option>
     <option value="fresh_food >Home</option>
     <option value="broth" >Kitchen</option>
    </select>
</div>

<script>
function producttypes(optiontype){
   //Move your logic here based on optiontype you will segregate
 if(optiontype == 'fresh_food"){
   var a = document.getElementById('cust_drop').value;
    document.getElementById('cust_drop').innerHTML ="";
    var options = `
    <option value="">--Select Category</option>
                        <option value="">TV & Appliances</option>
                      `
console.log('options',options);
document.getElementById('cust_drop').innerHTML = options;
}
if(optiontype=="broth"){
   //your logic goes here
}
}
</script>

Is a bad practice to try to use onclick with select. The select component is used for forms. If you need to use a dropdown, try to use this component instead of selecting it.尝试将 onclick 与 select 一起使用是一种不好的做法。select 组件用于 forms。如果您需要使用下拉列表,请尝试使用此组件而不是选择它。

I don't know why are you using the select component but I think that is not correct.我不知道你为什么要使用 select 组件,但我认为这是不正确的。

If you use select need to use jQuery the onChange event to capture de value of the option selected and do the correct action.如果使用 select 需要使用 jQuery 的 onChange 事件来捕获所选选项的 de 值并执行正确的操作。

$(document).ready(function(){
   $('#yourSelectId').on('change', function(){
       var option = $(this).value();

       //Or you can try this too.
       $( "#yourSelectId option:selected" ).value();

       //Use IF or Switch (I prefer switch normally)
       if(option == "fresh_food") home();
       else if(option == "broth") kitchen();
   });
});

I hope that this helps you.我希望这对你有帮助。

Kind regards.亲切的问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM