简体   繁体   English

jQuery显示div当单击单选按钮时使用id而不是value的div

[英]jQuery Show div when radio button is clicked with id instead of value

This code works, but not like I want it to work. 这段代码有效,但不像我希望的那样工作。 Now it is taking the value to open, I want it to use the id to open. 现在,它需要使用来打开,我希望它使用id来打开。 I can't use "Ja" constantly as value because i have multiple value's "Ja"(not shown in the code below). 我不能一直使用“ Ja”作为值,因为我有多个值的“ Ja”(未在下面的代码中显示)。

  $( document ).ready( function () { $( "input[name=group8" ).change( function() { var test8 = $( this ).val(); $( ".desc" ).hide(); $( "#" + test8 ).show(); } ); } ); 
 .desc { display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form name="form10" id="my_form10" method="post" action="" > <h5>Wil je keukenplinten laten inkorten na het leggen van de vloer?</h5> <div class="radio8"> <div class="janee"> <input id="JA7" type="radio" name="group8" value="Ja"> <label for="JA7" class="form-field__radio__label">Ja, meerprijs € 20.00 per plint</label><br> <input id="NEE7" type="radio" name="group8" value="Nee"> <label for="NEE7">Nee</label> </div> </div> </form> <form id="keukenplinten"> <div id="Ja" class="desc" style="float: inherit;"> <h5>Hoeveel keukenplinten wil je laten inkorten?</h5> <input type="number" id="keukenplintenInkorten" name="keukenplinten"> </div> </form> 

您可以尝试$(this).attr('id')代替$(this).val()

I guess you want the 'test8' variable to take the value of the element selected ID , in that case you can use: 我想您希望'test8'变量采用选定ID的元素的值,在这种情况下,您可以使用:

var test8 = $( this ).attr('id')

You can read here a detailed explanation: How can I get the ID of an element using jQuery? 您可以在此处阅读详细说明: 如何使用jQuery获取元素的ID?

Use this way 用这种方式

$( document ).ready( function () {
  $( "input[name=group8" ).change( function() {
    var test8 = $(this).attr('id')
    $( ".desc" ).hide();
    $( "#form_"  + test8 ).show();
  } );
} );

<form id="keukenplinten">
  <div id="form_JA7" class="desc" style="float: inherit;"> <!-- Change ID this Way-->
    <h5>Hoeveel keukenplinten wil je laten inkorten?</h5>
    <input type="number" id="keukenplintenInkorten" name="keukenplinten">
  </div>
</form>

http://jsfiddle.net/sj2bm5av/2/ http://jsfiddle.net/sj2bm5av/2/

If you want to use ID attribute of the clicked element for next steps, you can use following : 如果要使用clicked元素的ID属性进行下一步,则可以使用以下命令:

  $( document ).ready( function () {
  $( "input[name=group8" ).change( function() {
    var test8 = $( this ).attr("id");
    $( ".desc" ).hide();
    $( "#" + test8 ).show();
  } );
} );

But here test8 will have the element with ID "Ja7", that tries to call $( "#Ja7" ) . 但是在这里,test8将具有ID为“ Ja7”的元素,该元素尝试调用$( "#Ja7" ) This ID is used for radio element. 该ID用于无线电元素。 Hence you need to use a different ID for div ie " div_Ja7 " 因此,您需要为div使用不同的ID,即“ div_Ja7

PS Each element has a unique ID. PS每个元素都有一个唯一的ID。 You can't assign same ID to multiple elements. 您不能将相同的ID分配给多个元素。

I suggest you add a data-trigger (name by me) attribute to the .desc element. 我建议您向.desc元素添加一个data-trigger (由我命名)属性。

This says, which element has to have which value to show this element 这就是说,哪个元素必须具有哪个值才能显示此元素

 jQuery(function($) { $("input[name=group8").change(function() { var id = $(this).attr('id'); var val = $(this).val(); var selector = '[data-trigger="#' + id + ':' + val + '"]' $(".desc").hide(); $(selector).show(); }); }); 
 .desc { display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form name="form10" id="my_form10" method="post" action=""> <h5>Wil je keukenplinten laten inkorten na het leggen van de vloer?</h5> <div class="radio8"> <div class="janee"> <input id="JA7" type="radio" name="group8" value="Ja"> <label for="JA7" class="form-field__radio__label">Ja, meerprijs € 20.00 per plint</label><br> <input id="NEE7" type="radio" name="group8" value="Nee"> <label for="NEE7">Nee</label> </div> </div> </form> <form id="keukenplinten"> <div id="Ja" data-trigger="#JA7:Ja" class="desc" style="float: inherit;"> <h5>Hoeveel keukenplinten wil je laten inkorten?</h5> <input type="number" id="keukenplintenInkorten" name="keukenplinten"> </div> </form> 

I will suggest please introduce new attribute on "input" like "data-id" (it will allow assign same value for multiple input), see below code 我建议您在“输入”上引入新属性,例如“数据ID”(它将为多个输入分配相同的值),请参见以下代码

 <form name="form10" id="my_form10" method="post" action="" >
  <h5>Wil je keukenplinten laten inkorten na het leggen van de vloer?</h5>
  <div class="radio8">
    <div class="janee">
      <input id="JA7" data-id="ABC1" type="radio" name="group8" value="Ja">
      <label for="JA7" class="form-field__radio__label">Ja, meerprijs € 20.00 per 
       plint</label><br>
      <input id="NEE7" data-id="ABC2" type="radio" name="group8" value="Nee">
      <label for="NEE7">Nee</label>
    </div>
  </div>
</form>
<form id="keukenplinten">
  <div id="ABC1" class="desc" style="float: inherit;">
    <h5>Hoeveel keukenplinten wil je laten inkorten?</h5>
    <input type="number" id="keukenplintenInkorten" name="keukenplinten">
  </div>
</form>

And Use in Jquery: 并在Jquery中使用:

    $( document ).ready( function () {
      $( "input[name=group8" ).change( function() {
        var test8 = $( this ).attr("data-id");
        $( ".desc" ).hide();
        $( "#" + test8 ).show();
      });
   } );

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

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