jquery 如何用javascript知道一个活动单选按钮的值

3htmauhk  于 2023-02-03  发布在  jQuery
关注(0)|答案(1)|浏览(124)

我正在尝试检索一个单选按钮的值,而没有验证或按钮。
当用户更改单选按钮时,我希望检索实时值。
我已经尝试了很多代码,但没有工作.

var az = document.querySelector("input[name='promotioncases']").checked = true;
console.log(az);

var selectedValue = $("input.promotioncases:checked").val();
console.log(selectedValue);

var selectedRadioValue = $("input.promotioncases:checked").attr("radio-value");
console.log(selectedRadioValue);

const promotion = document.getElementsByName('promotioncases')
for (e of promotion) {
  if (e.checked)
    console.log(`Elément ${e.id} coché`)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input
  type="radio"
  name="promotioncases"
  class="promotioncases"
  id="black"
  data-target="10"
  data-nom="POSTE-ENVELLOPPE-belgique"
  value="10" />
  
<input
  type="radio"
  name="promotioncases"
  class="promotioncases"
  id="red"
  data-target="20"
  data-nom="POSTE-ENVELLOPPE-belgique"
  value="20" />

<input
  type="radio"
  name="promotioncases"
  class="promotioncases"
  id="vert"
  data-target="30"
  data-nom="POSTE-ENVELLOPPE-belgique"
  value="30" />
2ledvvac

2ledvvac1#

$('input[name="promotioncases"]').on('click', function() {
    console.log($(this).attr('data-target'))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input
  type="radio"
  name="promotioncases"
  class="promotioncases"
  id="black"
  data-target="10"
  data-nom="POSTE-ENVELLOPPE-belgique"
  value="10" />
  
<input
  type="radio"
  name="promotioncases"
  class="promotioncases"
  id="red"
  data-target="20"
  data-nom="POSTE-ENVELLOPPE-belgique"
  value="20" />

<input
  type="radio"
  name="promotioncases"
  class="promotioncases"
  id="vert"
  data-target="30"
  data-nom="POSTE-ENVELLOPPE-belgique"
  value="30" />

相关问题