[英]How to retrieve values in select tag within the radio button using jquery?
I have the following html radio tags. 我有以下html单选标记。 Basiclly, it has 3 radio button and the user can only pick one and I want the jquery code to pick up the value from the checked radio. 基本上,它有3个单选按钮,用户只能选择一个,我希望jquery代码从已检查的单选中获取值。
There is 3 radio button, one for all day, one for N/A and one for time. 有3个单选按钮,一个用于全天,一个用于N / A,一个用于时间。 And once user click time, it can selected the time between 12:00 to 20:00 in the tag. 用户单击时间后,便可以在标记中选择12:00到20:00之间的时间。
I know that using .val()
for extract the value for the other two options, but I am figuring out how to extract the values on the times when the time radio button is selected. 我知道使用.val()
提取其他两个选项的值,但是我正在弄清楚如何在选择时间单选按钮时提取时间值。
I am using Meteor framework for this project, but i guess this is a jQuery question. 我正在为此项目使用Meteor框架,但是我想这是一个jQuery问题。
html HTML
<tr>
<td>Monday</td>
<td><input type="radio" name="mon" value="all">all day</td>
<td>
<input type="radio" name="mon" value="time">between
<select name="firstTime">
<option>Time</option>
<option value="12">12:00</option>
<option value="13">13:00</option>
<option value="14">14:00</option>
<option value="15">15:00</option>
<option value="16">16:00</option>
<option value="17">17:00</option>
<option value="18">18:00</option>
<option value="19">19:00</option>
<option value="20">20:00</option>
</select>
to
<select name="secondTime">
<option>Time</option>
<option value="12">12:00</option>
<option value="13">13:00</option>
<option value="14">14:00</option>
<option value="15">15:00</option>
<option value="16">16:00</option>
<option value="17">17:00</option>
<option value="18">18:00</option>
<option value="19">19:00</option>
<option value="20">20:00</option>
</select>
</td>
<td><input type="radio" name="mon" value="n/a">N/A</td>
</tr>
You can do as below: 您可以执行以下操作:
$("input[name=mon]").on("change", function(){
if(this.value=="time"){
alert("First Time : " + $('select[name="firstTime"] option:selected').text());
alert("Second Time : "+$('select[name="secondTime"] option:selected').text());
}
});
UPDATE UPDATE
For this demo I am trying to get values on button click: 对于此演示,我试图通过单击按钮获取值:
$('.getVal').on('click',function(){
var selectedoption=$("input[name=mon]:checked").val().trim();
if(selectedoption=="time"){
var dispValue="First Time : " + $('select[name="firstTime"] option:selected').text()+ " and Second Time : "+$('select[name="secondTime"] option:selected').text();
$('.dispSelected').text(dispValue);
}
else
{
$('.dispSelected').text(selectedoption + " has been selected");
}
});
You can get their value when they change, for example: 当它们更改时,您可以获取它们的价值,例如:
jQuery("input[type=radio]").on("change", function(){
alert(this.value); //Will show the value of the selected radio
});
For getting the select
values, you can get them, for example, when the selects
change or when the radios
change. 要获取select
值,您可以获取它们,例如,当selects
更改或radios
更改时。 Check this fiddle . 检查这个小提琴 。
If you want to get the values when the form is submitted use the following code: 如果要在提交表单时获取值,请使用以下代码:
$('form').on('submit', function( e ) {
e.preventDefault();
var formData = $(this).serialize();//this holds all the form data
......
});
But to ensure that times are only included when the time
radio button is selected you need the following code: 但是要确保仅在选择time
单选按钮时才包括time
您需要以下代码:
$('input[name=mon]').on('change', function() {
$('select[name=firstTime], select[name=secondTime]').prop('disabled', this.value != 'time');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.