简体   繁体   English

选择选项值jquery

[英]select option value jquery

i have a bit of problem to deal with the option value in jquery. 我在jquery中处理选项值有一些问题。 which i have 2 kind of select and one of theme contain an array. 我有2种选择和主题之一包含一个数组。 some how i dont know how to compared them. 一些我不知道如何比较它们。

example: 例:

<select id="category">
   <option value="[1,2]"> category fruit</option>
   <option value="[3,4]"> category vegies</option>
</select>

<select id="article">
   <option value="1">banana</option>
   <option value="2">mango</option>
   <option value="3">letus</option>
   <option value="4">spinach</option>
</select>

script: 脚本:

 $(function(){
       var $article = $('#article > option').each(function(){
            //var $v = $(this).val();
            //console.log($v);
            return $(this).val();

       });

       $('#categorie').on('change', function(){
            var $e = $(this).val();
            console.log($e);
            if($e == $article){
                // here should be the value from the article was choosed from category
            }else{
                console.log('there are no entries')
            }

       });

 })

but i dont know how to compare theme properly. 但我不知道如何正确比较主题。 and it should be if #category [1,2] selected then the #article 1 and 2 only shown in option and the other should be hidden. 如果选择了#category [1,2],则应该仅在选项中显示#article 1和2,而另一个应隐藏。 any kind of idea or suggestion thank you very much. 任何想法或建议都非常感谢。 best regard. 最良好的问候。

try this: 尝试这个:

if($e.indexOf($article)!=-1){

}else{
    console.log('there are no entries')
}

First of all there is a typo categorie . 首先,有一个打字错误categorie It should be category 应该是category

Try this 尝试这个

var opts = [];
  $('#article > option').each(function(){

            opts.push($(this).val());

  });
  console.log(opts)
  $('#category').on('change', function(){
            var $e = $(this).val();
           var present = false;
           $(opts).each(function(i,v){
                    if($e[1]==v){
                    present= true;
                    console.log($e[1])
                  }
           });

           if(!present){
            console.log('there are no entries')
           }

       });

Demo 演示版

The value inside first select element is not an array, it just a string, remove bracket and leave only number with comma separated like following : first select元素内的值不是数组,它只是一个字符串,去掉方括号,仅用逗号分隔数字,如下所示:

HTML 的HTML

<select id="category">
 <option value="1,2"> category fruit</option>
 <option value="3,4"> category vegies</option>
</select>

<select id="article">
 <option value="">chose</option>
 <option value="1">banana</option>
 <option value="2">mango</option>
 <option value="3">letus</option>
 <option value="4">spinach</option>
</select>

Js (first option) Js (首选)

// use this
$('#category').change(function(){
  var cat = $( this ).val().split(',');
  $('#article option').hide()
  $.each(cat,function(i,e){
    $('#article option[value="'+e+'"]').show()
  })
});

(second option) (第二个选项)

//or use this : filter out only matched element
var cacheElem = $('#article option').detach();
$('#category').change(function(){
  $('#article option').remove();
  var cat = $( this ).val().split(',');  
  var h = cacheElem.filter(function(){
    return $.inArray(this.value, cat) !== -1 && this
  });
  $('#article').append(h)
}); 

DEMO 演示

Try this option, working example jsfiddle 试试这个选项,工作示例jsfiddle

$('#categorie').on('change', function(){
    var $e = $(this).val();
    $('#article > option').each(function(index, item) {
      if ($e.indexOf($(item).val()) === -1) {
        $(item).hide();
      } else {
        $(item).show();
      }
    });
   });

You need to get the select value [1,2] , then use JSON.parse to make that and array. 您需要获取选择值[1,2] ,然后使用JSON.parse将该值和数组组成。 Then you can search it. 然后您可以搜索它。

 var $article = []; $(function() { $article = $('#article > option').map(function() { return parseInt(this.value); }).get(); console.log($article); $('#category').on('change', function() { var $e = JSON.parse(this.value); console.log($e); valid = false; $.each($e, function(i, val){ valid = valid || $.inArray(val, $article) != -1; }); if (valid) alert('Exists' + $e.join()) else console.log('there are no entries') }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <select id="category"> <option value="[1,2]">category fruit</option> <option value="[3,4]">category vegies</option> </select> <select id="article"> <option value="1">banana</option> <option value="2">mango</option> <option value="3">letus</option> <option value="4">spinach</option> </select> 

I'm not sure of your flexibility to change the category option value attribute but I took the liberty to take another approach to your issue. 我不确定您是否可以灵活地更改类别选项值属性,但是我还是采取了另一种方法来解决您的问题。

I created an object with a list of possible items to display. 我创建了一个带有可能显示项目列表的对象。 The select id="category" options value is the name of the list of items I want to display. select id =“ category”选项值是我要显示的项目列表的名称。

When the category changes I update the article select with only the list of items I want to show. 当类别更改时,我将更新文章,并仅选择要显示的项目列表。

<select id="category">
   <option value="fruits"> category fruit</option>
   <option value="vegies"> category vegies</option>
</select>

<select id="article">
   <option value="1">banana</option>
   <option value="2">mango</option>
</select>

as for the JavaScript 至于JavaScript

$(function(){
                var items = {
          fruits: ["banana", "mango"],
          vegies: ["letus", "spinash"]
        };

       var current = items[0];

       var updateOptions = function(cat_in) {
        current = cat_in;
        output = "";

        for(var i = 0; i<items[cat_in].length; i++) {
          output += '<option value="'+i+'">'+items[cat_in][i]+'</option>'; 
        }

       return output;

       };

       $('#category').on('change', function(){
            var $e = $(this).val();
            if($e == current) return;

            var options = updateOptions($e);
            $("#article").empty().append(options);
       });

 });

Here is a live example: https://jsfiddle.net/marioandrade/mkt16n1g/1/ 这是一个实时示例: https : //jsfiddle.net/marioandrade/mkt16n1g/1/

You can use something like that. 您可以使用类似的方法。 It works for me. 这个对我有用。

Here is html. 这是html。

<select id="category">
   <option value="[1,2]"> category fruit</option>
   <option value="[3,4]"> category vegies</option>
</select>

<select id="article">
   <option value="1">banana</option>
   <option value="2">mango</option>
   <option value="3">letus</option>
   <option value="4">spinach</option>
</select>

And here is jquery. 这是jQuery。

$(document).ready(function(){
        function checkData(value){
            var $e = value;
            $('#article > option').each(function(index, item) {
                if ($e.indexOf($(item).val()) === -1) {
                    $(item).hide();
                } else {
                    $(item).show();
                }            
            });
        }
        $('#category').change(function(){
            checkData($(this).val());
        });

        checkData($('#category').val());
}); 

I think that may help you. 我认为这可能对您有帮助。

Thanks 谢谢

Seems pretty simple: 似乎很简单:

$('#category').on('change', function() {
  var currentCategory = $(this).val();
  // reset and show/hide group items
  $('#article').val("").find('option').each(function(index) {
    $(this).toggle(!(currentCategory.indexOf($(this).val()) === -1));
  });
}).triggerHandler('change');

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

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