首页 \ 问答 \ 如何将来自两个单选按钮的输入分配给单个字段?(How to assign the input from two radio buttons to a single field?)

如何将来自两个单选按钮的输入分配给单个字段?(How to assign the input from two radio buttons to a single field?)

我是编码新手。 我有两个单选按钮。 如果从它们中选择了“是”,则某个字段(此处为kouAP)必须自动设置为一个值(在本例中为0.56)。 问题是:

  1. 如何使两个单选按钮将值设置为单个字段?
  2. 如果其中一个是“是”,另一个是“否”,如何保持希望的价值? 不管点击的顺序如何。

我的JQuery没有任何意义:(

谢谢

HTML

<label for="Pre002">ccs</lable>  
<br />
<input type="radio" id="Pre002o" name="css" value="0">
<label for="Pre002o">none</label>
<input type="radio" id="Pre002d" name="css" value="4">
<label for="Pre002d">yes</label>
<br />
<label for="Pre066">mi</lable>  
<br />
<input type="radio" id="Pre066a" name="mi" value="0">
<label for="Pre066a">yes</label>
<input type="radio" id="Pre066b" name="mi" value="1">
<label for="Pre066b">no</label>
<br />
<input id="kouAP" type=“text” name="kouAP" readonly="true" placeholder="kouAP">
<label for="kouAP">at least one yes</label> 

JQuery的

$('input[type=radio].css; input[type=radio].mi').click(function(e){
  if ($(this).attr("id") == "Pre002d" || $(this).attr("id") == "Pre066a" ){
    $("#kouAP").val(0.5677075);
  }
  else {
    $("#kouAP").val(0);
  }
});

I am new in coding. I have two radio buttons. If a “yes” is selected in either from them a certain field (here kouAP) must be AUTOMATICALLY set to a value (in this case 0.56). The problems are:

  1. how to make both radio buttons to set the value to a single field?
  2. How to keep the wished value if one of the is “yes” and the other is “no”? No matter of the order of clicking.

My JQuery makes no sense :(

Thanks you

HTML

<label for="Pre002">ccs</lable>  
<br />
<input type="radio" id="Pre002o" name="css" value="0">
<label for="Pre002o">none</label>
<input type="radio" id="Pre002d" name="css" value="4">
<label for="Pre002d">yes</label>
<br />
<label for="Pre066">mi</lable>  
<br />
<input type="radio" id="Pre066a" name="mi" value="0">
<label for="Pre066a">yes</label>
<input type="radio" id="Pre066b" name="mi" value="1">
<label for="Pre066b">no</label>
<br />
<input id="kouAP" type=“text” name="kouAP" readonly="true" placeholder="kouAP">
<label for="kouAP">at least one yes</label> 

JQuery

$('input[type=radio].css; input[type=radio].mi').click(function(e){
  if ($(this).attr("id") == "Pre002d" || $(this).attr("id") == "Pre066a" ){
    $("#kouAP").val(0.5677075);
  }
  else {
    $("#kouAP").val(0);
  }
});

原文:https://stackoverflow.com/questions/47222761
更新时间:2022-11-24 10:11

最满意答案

代码没有问题。

只需提供您收听点击的元素的名称即可。

更换:

$('input[type=radio].css; input[type=radio].mi').click(...);

附:

$('input').click(...);

要么:

$('input[type=radio]').click(...);

以避免将来的错误。

我建议你再次完成基础知识:)

编辑

对于第二个问题,我想这只是一个if..else的工作。 希望能帮助到你。

$('input').click(function(e){
  if ($('#Pre002d').is(':checked') || $('#Pre066a').is(':checked')){
    $("#kouAP").val(0.5677075);
  }
    else{
        $("#kouAP").val(0);
    }
});

There is nothing wrong with code.

Just provide the name of the element you listen the click from.

Replace:

$('input[type=radio].css; input[type=radio].mi').click(...);

With:

$('input').click(...);

or:

$('input[type=radio]').click(...);

to avoid future errors.

I just advice you to go through the basics again :)

EDIT

For the second question, I guess it's just a work around with if..else. Hope it helps.

$('input').click(function(e){
  if ($('#Pre002d').is(':checked') || $('#Pre066a').is(':checked')){
    $("#kouAP").val(0.5677075);
  }
    else{
        $("#kouAP").val(0);
    }
});

相关问答

更多

相关文章

更多

最新问答

更多
  • 在页面树中使用不同的域(Use different domains in page tree)
  • 为什么这个半圈不会成长?(Why won't this semi circle grow?)
  • 如何学习计算机编程?
  • 如何在单个gcc命令中将多个头文件包和库目录添加到搜索路径?(How to add multiple header include and library directories to the search path in a single gcc command?)
  • VB.Net绑定datagridview Comboboxcolumn到datagridviewTextboxColumn(VB.Net binding datagridview Comboboxcolumn to datagridviewTextboxColumn)
  • 数据库设计 - 当一个记录有许多属性时(database design - when one record has many properties)
  • MATLAB函数计算两个坐标(纬度和经度)之间的距离(MATLAB function to calculate distance between two coordinates (latitude and longitude))
  • 如何从TFS中的团队项目查询所有PBI的历史变更?(How to query all PBI's historical changes from a team project in TFS?)
  • 使用点表示法的Python导入无法按预期工作(Python import using dot notation doesn't work as expected)
  • 我们能否将String列设置为partitionColumn?(Can we able to set String column as partitionColumn?)
  • 下划线模板无法按预期工作(Underscore template doesn't work as intended)
  • 怎么写项目开发的文档?
  • 为什么我的plpgsql函数没有返回任何行(why does my plpgsql function return no rows)
  • 补偿ARM中断的延迟?(compensating latency on ARM interrupts?)
  • Clojure:半平铺嵌套序列(Clojure: Semi-Flattening a nested Sequence)
  • Array方法返回所有null元素而不是object元素(Array method returning all null elements instead of object elements)
  • 禁止基于IP的用户,而不允许管理员访问IP(Ban user based on IP without allowing Admin to access IP)
  • c#wpf多重绑定不是很流行(c# wpf Multibindings not aviable)
  • 亚马逊Generic是什么意思
  • 如何在jgroup复制的hashmap中为状态转移提供超时?(How to give timeout for state transfer in jgroup replicated hashmap?)
  • highcharts可以生成这样的3d柱形图吗?(Can highcharts generate a 3d column chart like this?)
  • 在rails控制台中选择第二个条目?(Selecting second entry in rails console?)
  • 通过了解标记并且不循环遍历项目,将值设置为列表视图的某列中的行(Set a value to a row in a certain column of a listview by knowing the tag and without looping through items)
  • 杀死一个分叉的孩子(Kill a Forked Child)
  • 什么是代码点和代码空间?(What is a code point and code space?)
  • 如何在ruby中获取第一个html标记和第二个html标记之间的内容(how to get the content between the first html tag and the second html tag in ruby)
  • 需要在recyclerview中获取新添加项的视图参考(Need to get the view reference of newly added item in recyclerview)
  • 我今天部计算机出现故障啊,在按开机后,主机便发出“迪”的开机的报警声啊,在上午上过后就不行了,我只是把计算机的所有线重插过就这样,因为D线太乱啊,整理下。但到晚上计算机又能开机啊,可以详细告诉原因吗
  • 将字符串转换为NumPy datetime64 dtype(Convert string to NumPy datetime64 dtype)
  • 模块VB.Net中的命名空间(Namespace in module VB.Net)