简体   繁体   English

根据单选按钮选择隐藏或显示值

[英]Hiding or showing value based on radio button selection

I have created a form and part of it requires the user to select whether they would like collection or postage. 我创建了一个表格,其中的一部分要求用户选择他们想要收款还是邮资。 What I have partially works but because the the value for one of the radio buttons is 7.25 it does not work for some reason. 我已经部分工作了,但是因为单选按钮之一的值是7.25,所以由于某种原因它不起作用。

This the radio button part of the form: 这是单选按钮形式的一部分:

<p>Would you like postage(£7.25 extra) or to collect from one of our shops?</p>
<label class="checkbox-inline"></label>
    <input type="radio" required name="delivery[]" class="photo" id="delivery" value="7.25">Postage
</label>
<label class="checkbox-inline">
<input type="radio" name="delivery[]" class="photo" id="collection" value="0"/>Collection

    <div id="7.25" class="desc">
       You have chosen postage
    </div>
    <div id="0" class="desc">
       You have chosen collection
    </div>

And this is the script I have at the moment: 这是我目前拥有的脚本:

<script type="text/javascript">
$(document).ready(function() {
    $("div.desc").hide();
    $("input[name$='delivery[]']").click(function() {
        var choice = $(this).val();
        $("div.desc").hide();
        $("#" + choice).show();
    });
});
</script>

Like I said this would work if the value was a whole number but because of the . 就像我说的那样,如果值是一个整数但由于,这将起作用。 it does not for some reason. 它不是出于某种原因。 Why is this the case and how do I sort this? 为什么会这样,我该如何分类? The values of the radio buttons are numbers as this affects the total price 单选按钮的值是数字,因为这会影响总价

When you call $("#7.25") it believes that 7 is the ID and 25 is a class, because . 当您调用$("#7.25")它认为7是ID,而25是类,因为. represents a class. 代表一个班级。

Also, you should not use numbers as ID. 另外,您不应使用数字作为ID。 If you do want a number in the ID, start with a letter like k_7 如果确实要在ID中输入数字,请以类似k_7的字母k_7

There are many ways to fix this, in the example below, I've added data-descTarget="k_0" to the inputs and changed the ids of the div's. 有很多方法可以解决此问题,在下面的示例中,我向inputs添加了data-descTarget="k_0"并更改了div的ID。

Demo 演示版

 $(document).ready(function() { $("div.desc").hide(); $("input[name$='delivery[]']").click(function() { var choice = $(this).attr("data-descTarget"); $("div.desc").hide(); $("#" + choice).show(); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p>Would you like postage(£7.25 extra) or to collect from one of our shops?</p> <label class="checkbox-inline"></label> <input type="radio" required name="delivery[]" class="photo" id="delivery" data-descTarget="k_0" value="7.25">Postage </label> <label class="checkbox-inline"> <input type="radio" name="delivery[]" class="photo" id="collection" data-descTarget="k_1" value="0"/>Collection <div id="k_0" class="desc"> You have chosen postage </div> <div id="k_1" class="desc"> You have chosen collection </div> 

This will help you. 这将为您提供帮助。 as you cant put id with . 因为您不能将ID与. and you should not use number as id. 并且您不应使用数字作为ID。 you could use data attribute. 您可以使用数据属性。

<p>Would you like postage(£7.25 extra) or to collect from one of our shops?</p>
<label class="checkbox-inline">
    <input type="radio" required name="delivery[]" class="photo" id="delivery" value="7.25">Postage
</label>
<label class="checkbox-inline">
<input type="radio" name="delivery[]" class="photo" id="collection" value="0"/>Collection
</label>
<div data-id="7.25" class="desc">
  You have chosen postage
</div>
<div data-id="0" class="desc">
  You have chosen collection
</div>

script 脚本

$(document).ready(function() {
    $("div.desc").hide();
    $("input[name$='delivery[]']").click(function() {
        var choice = $(this).val();
        $("div.desc").hide();
       $('*[data-id="'+choice+'"]').show();
    });
});

https://jsfiddle.net/8hmuk0xg/ https://jsfiddle.net/8hmuk0xg/

You can't select #7.25 in jQuery, but you can do it using vanillaJS! 您无法在jQuery中选择#7.25,但可以使用vanillaJS进行选择!

intead of $("#" + choice).show(); $("#" + choice).show();

Write this $(document.getElementById(choice)).show(); 写下这个$(document.getElementById(choice)).show();

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

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