简体   繁体   English

如何使用表中的javascript检查值是否存在

[英]How to check value exist in table use javascript

<select name="product">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>

<table id="ListProduct">
<tr data-id="1"><td>A</td></tr>
<tr data-id="4"><td>D</td></tr>
<tr data-id="5"><td>E</td></tr>
<tr data-id="6"><td>F</td></tr>
<tr data-id="7"><td>G</td></tr>
</table>

And my javascript: 而我的javascript:

$('select[name=product]').on('change', function(){
   var pid = $('select[name=product]').val();
   var table = $('#ListProduct');
   var check_value = $(table).find("tr").data('id').eq(pid);
   if(check_value) alert("ID exist") else alert("Not exist");
});

When I select value=1, bring error. 当我选择value = 1时,带来错误。 How to fix it? 如何解决?

try this: http://jsfiddle.net/leojavier/1bnkfb06/ 试试这个: http : //jsfiddle.net/leojavier/1bnkfb06/

$('select[name=product]').on('change', function(){
   var pid = $(this).val();
   var table = $('#ListProduct');
   var check_value = $(table).find("tr").data('id');

   if(check_value == pid) {
       console.log("ID exist") 
   }
       else {
           console.log("Not exist");
       }
});

Add a new option to ensure that the ID for A can also be found 添加一个新选项以确保还可以找到A的ID

 $('select[name=product]').on('change', function(){ var pid = $(this).val(); var table = $('#ListProduct'); var check_value = $(table).find("tr").data('id'); (check_value == pid)?alert('ID exists'):alert('ID does not exist');//check if id exists }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="product"> <option>select</option> <option value="1">A</option> <option value="2">B</option> <option value="3">C</option> </select> <table id="ListProduct"> <tr data-id="1"><td>A</td></tr> <tr data-id="4"><td>D</td></tr> <tr data-id="5"><td>E</td></tr> <tr data-id="6"><td>F</td></tr> <tr data-id="7"><td>G</td></tr> </table> 

You get an error as .data('id') in your code returns an integer which doesn't have .eq method. 由于代码中的.data('id')返回了没有.eq方法的整数,因此会出现.eq .eq is a method of the jQuery object. .eq是jQuery对象的方法。

Also note that .data as getter returns value of the first element in the collection. 另请注意, .data作为getter返回集合中第一个元素的值。 You should either iterate through the tr collection and filter the matching elements or use the power of the jQuery selector engine: 您应该遍历tr集合并过滤匹配的元素,或者使用jQuery选择器引擎的功能:

$('select[name=product]').on('change', function() {
   var $tr = $('#ListProduct tr[data-id="'+ this.value +'"]');

   if ( $tr.length ) {
      alert("ID exist");
   } else { 
      alert("Not exist");
   }
});

You need to check wether the value of product matches the data id in listproduct like this. 您需要检查product的值是否与listproduct中的数据ID相匹配,就像这样。 http://jsfiddle.net/38e2h5ao/ http://jsfiddle.net/38e2h5ao/

$('select[name=product]').on('change', function () {
    var pid = $('select[name=product]').val();
    var table = $('#ListProduct');
    var exist;
    $(table).find("tr").each(function () {
        var check_value = $(this).data('id');
        if (check_value == pid) {
            exist = true;
        }
    });

    if (exist) alert("ID exist");
    else alert("Not exist");
});

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

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