Javascript jquery从div输入更改单选值
我需要将一个数字除以3个不同的数字,然后我需要将该值赋给单选按钮。这是我的密码。我有从单选按钮捕获值的脚本,但没有更改它Javascript jquery从div输入更改单选值,javascript,jquery,html,Javascript,Jquery,Html,我需要将一个数字除以3个不同的数字,然后我需要将该值赋给单选按钮。这是我的密码。我有从单选按钮捕获值的脚本,但没有更改它 //Radio And Div <input type="radio" name=presets class="m" value=""> <div id="choice_month" class="m"></div> <input type="radio" name=presets class=
//Radio And Div
<input type="radio" name=presets class="m" value="">
<div id="choice_month" class="m"></div>
<input type="radio" name=presets class="q" value="">
<div id="choice_quart" class="q"></div>
<input type="radio" name=presets class="y" value="">
<div id="choice_year" class="y"></div>
请用id修改类,因为这种HTML方式是不合法的(对不同的元素使用相同的id)
当您单击
div时,它将获得该div的ID,然后将其添加到单选按钮的值中。我在您这样编写的代码中有点迷失了。它没有样式,但符合你的要求
$("#button").click(function() {
$("#amount").trigger("change");
});
$("#amount").change(function() {
var num1 = $("input[id='amount']").val(),
num2 = $("input[id='m']").val(),
num3 = $("input[id='q']").val(),
num4 = $("input[id='y']").val(),
result = 0;
result = parseFloat(num1, 10) / parseFloat(num2, 10);
$("#choice_m").text(result.toFixed(2));
result = parseFloat(num1, 10) / parseFloat(num3, 10);
$("#choice_q").text(result.toFixed(2));
result = parseFloat(num1, 10) / parseFloat(num4, 10);
$("#choice_y").text(result.toFixed(2));
$("input[name='presets']:first").trigger("change");//trigger our on-change event in case the radio has been selected BEFORE the button was clicked.
});
$("input[name='presets']").change(function() {
var id = "#choice_"+$("input[name='presets']:checked").attr("id");
$("#result").val($(id).text());
})
ID应该是唯一的,最好不是数字。另外,jQuery实际上没有使用HTML中使用的任何ID/类……ID中也不能有空格;)你能为我们做一个报价吗?。你的输入名称需要在值周围加上引号。。。不是强制性的,但被认为是良好的做法。这里是:我不理解这个答案。我需要将class=“m”值转换为单选值。您可以在代码中看到,单击的div
的类通过参数传递给单选按钮的值。它只是更改元素的属性值。它应该能用…这么干净。!!我正在尝试混合另一个额外的代码。
<input type="radio" name="presets" class="m" value="">
<div id="choice_month" class="m"></div>
$('div').click(function () {
$('input[type=radio]').attr('value', $(this).attr('class'));
}
$("#button").click(function() {
$("#amount").trigger("change");
});
$("#amount").change(function() {
var num1 = $("input[id='amount']").val(),
num2 = $("input[id='m']").val(),
num3 = $("input[id='q']").val(),
num4 = $("input[id='y']").val(),
result = 0;
result = parseFloat(num1, 10) / parseFloat(num2, 10);
$("#choice_m").text(result.toFixed(2));
result = parseFloat(num1, 10) / parseFloat(num3, 10);
$("#choice_q").text(result.toFixed(2));
result = parseFloat(num1, 10) / parseFloat(num4, 10);
$("#choice_y").text(result.toFixed(2));
$("input[name='presets']:first").trigger("change");//trigger our on-change event in case the radio has been selected BEFORE the button was clicked.
});
$("input[name='presets']").change(function() {
var id = "#choice_"+$("input[name='presets']:checked").attr("id");
$("#result").val($(id).text());
})