两个单独的选择选项不应该使用javascript冲突

时间:2017-06-01 06:49:52

标签: javascript php jquery

任何人都可以帮我弄清楚我写的代码出了什么问题, 我有两个单独的选择选项,我想要两个选择选项之间的搜索值

HTML:

<select style="font-size:18px" id="sg" name="sg" onchange="SetText(sg,word1)">
    <option value="all" selected="selected">Select</option>
    <option value="icpa_2039">icpa_2039</option>
    <option value="icpl_131">icpl_131</option>
    <option value="icpl_96061">icpl_96061</option> 
</select>
between 
<select style="font-size:18px" id="sg2" name="sg2" onchange="SetText(sg2,word2)">
    <option value="all" selected="selected">Select</option>
    <option value="icpa_2039">icpa_2039</option>
    <option value="icpl_131">icpl_131</option>
    <option value="icpl_96061">icpl_96061</option> 
  </select>

JS:

<script src="js/jquery-1.9.0.min.js"></script>
<script>
    var $j = jQuery.noConflict();
    $j(function(){
      $j("select[id^='sg']").change(function(){
        var geno1 =$j('#sg').val();
        var geno2 =$j('#sg2').val();

        if(geno1==geno2) {
          $j('#warning').show();
          $j('#warning').html("Please choose two differnet genotypes names!!!");
        }else {
          $j('#warning').hide();
        }
      });
    });
</script>

3 个答案:

答案 0 :(得分:0)

如果下拉列表中的选定值相同,则警告div中将显示错误消息。希望我理解正确。以下是代码:

var $j = jQuery.noConflict();
$j(function() {
  $j("select[id^='sg']").change(function() {
    var geno1 = $j('#sg').val();
    var geno2 = $j('#sg2').val();
    $j('#warning').hide();
    if (geno1 == geno2) {
      $j('#warning').show().html("Please choose two differnet genotypes names!!!");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td> <select style="font-size:18px" id="sg" name="sg">
<option value="all" selected="selected">Select</option>
<option value="icpa_2039">icpa_2039</option>
<option value="icpl_131">icpl_131</option>
<option value="icpl_96061">icpl_96061</option> </select> </td>
<td> between </td>
<td> <select style="font-size:18px" id="sg2" name="sg2">
<option value="all" selected="selected">Select</option>
<option value="icpa_2039">icpa_2039</option>
<option value="icpl_131">icpl_131</option>
<option value="icpl_96061">icpl_96061</option> </select></td>
<div id="warning"></div>

答案 1 :(得分:0)

没有问题。仅从onchange中删除select属性并尝试使用agin。

试试这个:Demo

<强> HTML:

<select style="font-size:18px" id="sg" name="sg">
  <option value="all" selected="selected">Select</option>
  <option value="icpa_2039">icpa_2039</option>
  <option value="icpl_131">icpl_131</option>
  <option value="icpl_96061">icpl_96061</option> 
</select>
between 
<select style="font-size:18px" id="sg2" name="sg2">
  <option value="all" selected="selected">Select</option>
  <option value="icpa_2039">icpa_2039</option>
  <option value="icpl_131">icpl_131</option>
  <option value="icpl_96061">icpl_96061</option> 
</select>
<div id="warning">
</div>

Jquery:

var $j = jQuery.noConflict();
$j(function(){
  $j("select[id^='sg']").change(function(){
    var geno1 =$j('#sg').val();
    var geno2 =$j('#sg2').val();

    if(geno1==geno2) {
      $j('#warning').show();
      $j('#warning').html("Please choose two differnet genotypes names!!!");
    }else {
      $j('#warning').hide();
    }
  });
});

答案 2 :(得分:0)

您的代码似乎没问题。

请确保您格式化代码。

 var $j = jQuery.noConflict();
 $j(function() {
       $j("select[id^='sg']").change(function() {
         var geno1 = $j('#sg').val();
         var geno2 = $j('#sg2').val();
         if (geno1 == geno2) {
           $j('#warning').show();
           $j('#warning').html("Please choose two differnet genotypes names!!!");
         } else {
           $j('#warning').hide();
         }
       });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<td>
  <select style="font-size:18px" id="sg" name="sg">
    <option value="all" selected="selected">Select</option>
    <option value="icpa_2039">icpa_2039</option>
    <option value="icpl_131">icpl_131</option>
    <option value="icpl_96061">icpl_96061</option>
  </select>
</td>
<td> between </td>
<td>
  <select style="font-size:18px" id="sg2" name="sg2">
    <option value="all" selected="selected">Select</option>
    <option value="icpa_2039">icpa_2039</option>
    <option value="icpl_131">icpl_131</option>
    <option value="icpl_96061">icpl_96061</option>
  </select>
</td>
<div id='warning'>Warning</div>