gpt4 book ai didi

javascript - 单选按钮选择触发 JavaScript 函数

转载 作者:行者123 更新时间:2023-12-02 15:07:18 25 4
gpt4 key购买 nike

我希望该函数根据选择/检查的 radio 输入来触发另一个函数。我的功能似乎没有“看到”单选按钮检查事件。 我做错了什么?这只是一个摘录,函数chartAmount()和chartPercent()是在原文中定义的。

<html>
<head>
<title>Connect Radio Sliders to JS</title>
<script src="http://public.tableausoftware.com/javascripts/api/tableau_v8.debug.js"></script>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="semantic.min.css">
</head>
<body>
<h1>Radio Buttons fire JS Function</h1>
<hr>
<h3>Problem/Question</h3>
<hr>
<p>I want the selection of a radio button to trigger a javascript function. My attempt does not work, what am I am doing wrong/missing?</p>
<hr>
<h3>Note:</h3>
<p>The HTML and JS are extracted from a larger filet that include all approriate links to reuqired resources. The two functions chartAmount() and chartPercent() are defined in the larger document.</p>
<hr>
<hr>
<div class="ui form">
<div class="grouped fields">
<label>Radio Button Select</label>
<div class="field">
<div class="ui slider checkbox">
<input type="radio" name="charttype" checked="checked" value="percent">
<label>Amount by Year</label>
</div>
</div>
<div class="field">
<div class="ui slider checkbox">
<input type="radio" name="charttype" value="amount">
<label>Percent by Year</label>
</div>
</div>
</div>
</div>
<script>
$('.field').click(function() {
if($("input[value='Amount']").is(':checked')) { chartAmount(); }
if($("input[value='Percent']").is(':checked')) { chartPercent(); }
})
</script>
</body>

最佳答案

选择器区分大小写。您应该使用:

$('.field').click(function() {
if($("input[value='amount']").is(':checked')) { chartAmount(); }
if($("input[value='percent']").is(':checked')) { chartPercent(); }
})

编辑添加:

您可能还需要考虑绑定(bind)到 input[type="radio"] 的更改事件。您还需要将标签与相应的输入绑定(bind),否则它们不会提供任何值。

更像这样:

Javascript:

$('input[type="radio"]').on('change',function() { 
if($("input[value='amount']").is(':checked')) { chartAmount(); }
if($("input[value='percent']").is(':checked')) { chartPercent(); }
})

HTML:

<div class="grouped fields">
Radio Button Select
<div class="field">
<div class="ui slider checkbox">
<label><input type="radio" name="charttype" checked="checked" value="percent"> Amount by Year</label>
</div>
</div>
<div class="field">
<div class="ui slider checkbox">
<label><input type="radio" name="charttype" value="amount"> Percent by Year</label>
</div>
</div>
</div>

JS fiddle 示例:https://jsfiddle.net/eo0e4r63/

关于javascript - 单选按钮选择触发 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35023430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com