【问题标题】:same values should not be selected in two dropdown list of the same table row不应在同一表格行的两个下拉列表中选择相同的值
【发布时间】:2018-11-15 18:37:44
【问题描述】:

在表格中,我在两列中有下拉列表。 我正在进行客户端验证,我应该显示弹出消息 如果用户从下拉列表中选择同一行中的“选择产品1”和“选择产品2”列中的相同值。

演示链接:http://plnkr.co/edit/oxo8UvtNwyjGSR8y05U3?p=preview

在演示链接中,当用户为 PID 100 的“Select Product1”和“Select Product2”列选择笔记本电脑和笔记本电脑时, 它按预期显示带有消息的弹出窗口,但是下次当用户在其他行中选择其他值时仍然 它正在显示弹出窗口。

注意:如果用户在 Select Product1 和 Select Product2 列中选择相同的产品,我的要求是 每一行(PID) 应显示一个弹出对话框,并显示消息“两个下拉列表中的产品名称不能相同..”

//populate dropdown with the value
  function populateSelect(ids) {
     var ids = [{"pid":"laptop","des":"laptop"},{"pid":"Mobile","des":"Mobile"},{"pid":"IPAD mini.","des":"IPAD mini."}]
      var productDropdown1 = $(".product1");
      var productDropdown2 = $(".product2");
      $.each(ids, function(index,value) {
      $("<option />").text(value.des).val(value.pid).appendTo(productDropdown1);
        $("<option />").text(value.des).val(value.pid).appendTo(productDropdown2);
      });
    
    //onchange of the dropdown list 
     $("select").change(function()
     { 
       //var optionSelected = $("option:selected", this);
       //var valueSelected = this.value;
        var selectedText = $(this).val();
        console.log("selectedText " + selectedText);
        var product1_drop = $('.product1').val(); // Get selected value of product1 dropdown
        var product2_drop = $('.product2').val(); // Get selected value of product2 dropdown
        console.log("product1_drop " + product1_drop);
        console.log("product2_drop " + product2_drop);
           if(product1_drop == product2_drop ){       
                  alert('Product name cannot be same..');
                 }
      });
}
 
$(document).ready(function(){
    populateSelect(); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="productTable" border="1">
    <thead>
    <tr>
        <th>PID</th>
        <th>Select Product1</th>
        <th>Select Product2</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>100</td>
        <td>
            <select class="product1" >
                <option value=""></option>
            </select>
        </td>
        <td>
            <select class="product2" >
                <option value=""></option>
            </select>
        </td>
    </tr>
    <tr>
        <td>200</td>
        <td>
            <select class="product1" >
                <option value=""></option>
            </select>
        </td>
        <td>
            <select class="product2" >
                <option value=""></option>
            </select>
        </td>
    </tr>
    <tr>
        <td>300</td>
        <td>
            <select class="product1" >
                <option value=""></option>
            </select>
        </td>
        <td>
            <select class="product2" >
                <option value=""></option>
            </select>
        </td>
    </tr>
    <tr>
        <td>400</td>
        <td>
            <select class="product1" >
                <option value=""></option>
            </select>
        </td>
        <td>
            <select class="product2" >
                <option value=""></option>
            </select>
        </td>
    </tr>
    </tbody>
</table>

【问题讨论】:

  • 你试过修整和降低大小写吗?
  • 我提供了相同的列表以显示在两列下拉列表中。所以我相信这不是案例的问题..
  • 问题是 product1 和 product2 元素有多个,val() 不返回数组,也不向下过滤找到第一个有值的元素。它只会返回结果堆栈中第一个元素的值
  • 鉴于存在多个 product1 和 product2 元素,您需要提供更多详细信息以说明哪些确切情况应符合您的条件。例如,如果在不同的行上选择了相同的值,是否应该被视为匹配,或者它仅对同一行中的下拉菜单有效。
  • 好的,那么$(this).closest('tr') 将为您提供已更改的下拉列表所属的行。然后关闭那个人,你只需要找到属于它的 product1 和 product2 进行比较

标签: javascript jquery html


【解决方案1】:

使用以下代码时,它按预期工作:

 var row = $(this).closest("tr");
 var product1_drop = $('.product1',row).val(); // Get selected value of product1 dropdown
 var product2_drop = $('.product2',row).val();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多