我正在开发一个产品比较器,其中用户必须 Select 只有4个产品的宇宙中的两个产品,例如.

在这种情况下,用户将在这个世界中 Select 具有4个产品的两个产品,并且这些 Select 的产品的信息将出现在列中(列1和列2).

我试着在这里研究一些类似的问题,但不幸的是,我没有找到正确的方法:

  1. Get two values from select option at once
  2. Select and Radio options to show div
  3. Two radio buttons are selecting at once

我的问题是:我如何才能只 Select 两个单选选项,始终只限制两个选项?另外,如何显示列1内所选选项和列2内第二个所选产品的单选数值?

以下是我使用的代码:

HTML structure that display 4 products with their respective radio buttons:

<h2 class="mb-3">Produtos</h2>
  
<div class="row justify-content-center">
  
<div class="col-2">

<img src="https://assets.adidas.com/images/h_840,f_auto,q_auto,fl_lossy,c_fill,g_auto/cde9362a09ba4dd38c9ead6600ac074e_9366/Tenis_Duramo_SL_2.0_Preto_GW8336_01_standard.jpg" width="100px"/>

<input class="form-check-input radio" type="radio" name="produto1" id="1" value="Tênis preto">
<label class="form-check-label" for="1"></label>

</div>

<div class="col-2">

<img src="https://assets.adidas.com/images/h_840,f_auto,q_auto,fl_lossy,c_fill,g_auto/7ed0855435194229a525aad6009a0497_9366/Tenis_Superstar_Branco_EG4958_01_standard.jpg" width="100px"/>
    
<input class="form-check-input radio" type="radio" name="produto2" id="2" value="Tênis branco">
<label class="form-check-label" for="2"></label>
  
</div>

<div class="col-2">

<img src="https://assets.adidas.com/images/h_840,f_auto,q_auto,fl_lossy,c_fill,g_auto/9326e8db8d8e4e509e42ad26010cf693_9366/Tenis_adidas_4DFWD_Pulse_Preto_Q46451_01_standard.jpg" width="100px"/>
    
<input class="form-check-input radio" type="radio" name="produto3" id="3" value="Tênis verde">
<label class="form-check-label" for="3"></label>

</div>

<div class="col-2">

<img src="https://assets.adidas.com/images/h_840,f_auto,q_auto,fl_lossy,c_fill,g_auto/7a80a0e74201457eb1e5adcb00ff92f8_9366/Tenis_Dropset_Trainer_Azul_GZ2941_01_standard.jpg" width="100px"/>
    
<input class="form-check-input radio" type="radio" name="produto4" id="4" value="Tênis azul">
<label class="form-check-label" for="4"></label>

</div>

</div>

<div id="coluna1"></div>
<div id="coluna2"></div>

下面的jQuery代码获取所选项目的值​​,并在各自的列中显示它们的信息.这些编码不允许 Select 两个以上的产品:

$('.radio').change(function(){
    
    var shoes = $(this).val();
    var count = $("input[type='radio']:checked").length;
    
    if(count > 2){
        
    alert("you just have to select only two shoes.");
      
        $(this).prop('checked', false);
    
    return false;
      
    }
  
    $('#column1').text(shoes);
    $('#column2').text(shoes);
    
    // 

});

在这种情况下,我如何改进我的代码以达到正确的结果?作为一个实际例子,我起草了一份关于JSFIDLE:https://jsfiddle.net/kg51Lvzx/的草案

推荐答案

为了满足您的要求,我建议您使用复选框而不是单选控件.这允许用户取消 Select 选项.

从那里,您可以遍历所选项目,以在相关div中输出它们的文本.

另外,还有几个关于HTML的备注.首先,我将重复的元素改为使用相同的类,而不是递增的ID,因为这使 Select 变得容易得多.其次,这label个元素现在包装了相关内容.这意味着可以单击CheckBox控件和图像来进行 Select .

let $output = $('.output');

let updateSelections = () => {
  $output.text('');
  $products.filter(':checked').each((i, el) => {
    $output.eq(i).text(el.value);
  });
}

let $products = $('.product').change(function() {
  if ($products.filter(':checked').length > 2) {
    $(this).prop('checked', false);
    alert("you just have to select only two shoes.");
  } else {
    updateSelections();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" />
<h2 class="mb-3">Products - shoes</h2>
<div class="row justify-content-center">
  <div class="col-2 ms-2">
    <label class="form-check-label">
      <img src="https://assets.adidas.com/images/h_840,f_auto,q_auto,fl_lossy,c_fill,g_auto/cde9362a09ba4dd38c9ead6600ac074e_9366/Tenis_Duramo_SL_2.0_Preto_GW8336_01_standard.jpg" width="100px" />
      <div>
        <input class="form-check-input product" type="checkbox" name="product1" id="1" value="Black shoes">
      </div>
    </label>
  </div>
  <div class="col-2 ms-2">
    <label class="form-check-label">
      <img src="https://assets.adidas.com/images/h_840,f_auto,q_auto,fl_lossy,c_fill,g_auto/7ed0855435194229a525aad6009a0497_9366/Tenis_Superstar_Branco_EG4958_01_standard.jpg" width="100px" />
      <div>
        <input class="form-check-input product" type="checkbox" name="product2" id="2" value="White shoes">
      </div>
    </label>
  </div>
  <div class="col-2 ms-2">
    <label class="form-check-label">
      <img src="https://assets.adidas.com/images/h_840,f_auto,q_auto,fl_lossy,c_fill,g_auto/9326e8db8d8e4e509e42ad26010cf693_9366/Tenis_adidas_4DFWD_Pulse_Preto_Q46451_01_standard.jpg" width="100px" />
      <div>
        <input class="form-check-input product" type="checkbox" name="product3" id="3" value="Green shoes">
      </div>
    </label>
  </div>
  <div class="col-2 ms-2">
    <label class="form-check-label">
      <img src="https://assets.adidas.com/images/h_840,f_auto,q_auto,fl_lossy,c_fill,g_auto/7a80a0e74201457eb1e5adcb00ff92f8_9366/Tenis_Dropset_Trainer_Azul_GZ2941_01_standard.jpg" width="100px" />
      <div>
        <input class="form-check-input product" type="checkbox" name="product4" id="4" value="Blue shoes">
      </div>
   </label>
  </div>
</div>
<div class="output" id="column1"></div>
<div class="output" id="column2"></div>

Javascript相关问答推荐

订阅操作顺序

HTML/JavaScript函数未执行

用相器进行向内碰撞检测

使用AJX发送表单后,$_Post看起来为空

我在这个黑暗模式按钮上做错了什么?

colored颜色 检测JS,平均图像 colored颜色 检测JS

我们如何从一个行动中分派行动

屏幕右侧屏障上的产卵点""

html + java script!需要帮助来了解为什么我得到(无效的用户名或密码)

我的角模板订阅后不刷新'

我怎么在JS里连续加2个骰子的和呢?

在画布中调整边上反弹框的大小失败

面对代码中的错误作为前端与后端的集成

当代码另有说明时,随机放置的圆圈有时会从画布上消失

如何用javascript更改元素的宽度和高度?

基于产品ID更新条带产品图像的JavaScript命中错误

将范围 Select 器添加到HighChart面积图

如何使用抽屉屏幕及其子屏幕/组件的上下文?

是否可以在不更改组件标识的情况下换出Reaction组件定义(以维护状态/引用等)?如果是这样的话,是如何做到的呢?

ReferenceError:无法在初始化之前访问setData