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

时间:2019-08-06 11:49:27

标签: jquery html

我已经创建了一个表格,其中的一部分要求用户选择他们想要收款还是邮资。我已经完成了部分工作,但是因为单选按钮之一的值为7.25,由于某种原因它不起作用。

这是表单的单选按钮部分:

In [17]: s = a.sum()

In [18]: c = np.zeros((s,len(a)),dtype=b.dtype)

In [20]: c[np.arange(s),np.flatnonzero(a)] = 1

In [21]: np.vstack((b,c))
Out[21]: 
array([[1, 2, 3, 4, 5],
       [1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0]])

这是我目前拥有的脚本:

<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>

就像我说的那样,如果该值是一个整数,那将是可行的,但是由于。它不是出于某种原因。为什么会这样,我该如何分类?单选按钮的值是数字,因为这会影响总价格

3 个答案:

答案 0 :(得分:4)

当您致电$("#7.25")时,它会认为7是ID,25是一个类,因为.代表一个类。

此外,您不应使用数字作为ID。如果确实要在ID中输入数字,请以类似k_7

的字母开头

有很多方法可以解决此问题,在下面的示例中,我将data-descTarget="k_0"添加到了inputs并更改了div的ID。

演示

$(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>

答案 1 :(得分:1)

这将为您提供帮助。因为您不能将id与.放在一起,因此不应使用数字作为id。您可以使用数据属性。

<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>

脚本

$(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/

答案 2 :(得分:0)

您无法在jQuery中选择#7.25,但可以使用vanillaJS做到这一点!

$("#" + choice).show();的内页

将此$(document.getElementById(choice)).show();

写入