通过jQuery获取所需的选择框值 [英] Get desired value of selectbox through jquery
问题描述
在这里,我有三个下拉菜单,其中包含不同的值,并且代码如下所示.
Here i have three dropdowns with differents values and the code looks like as follows.
要使用jquery来获取相应下拉列表的值,我总是有这个值,但是请始终查看
To get the value of the corresponding dropdown using jquery i had this but it always gets same value please have a look
$(".one").hide();
$(".two").hide();
$(".three").show();
var name_type=$("#abc_type_1").attr('name','type');
$("#abc_type_2").removeAttr('name');
$("#abc_type_3").removeAttr('name');
$("input[name=select_type]:radio").change(function () {
if($(this).val()=='1')
{
$(".one").show();
$(".two").hide();
$(".three").hide();
var name_type=$("#abc_type_1").attr('name','type');
$("#abc_type_2").removeAttr('name');
$("#abc_type_3").removeAttr('name');
}
else if($(this).val()=='2')
{
$(".one").hide();
$(".two").show();
$(".three").hide();
var name_type=$("#abc_type_2").attr('name','type');
$("#abc_type_1").removeAttr('name');
$("#abc_type_3").removeAttr('name');
}
else if($(this).val()=='3')
{
$(".one").hide();
$(".two").hide();
$(".three").show();
var name_type=$("#abc_type_3").attr('name','type');
$("#abc_type_1").removeAttr('name');
$("#abc_type_2").removeAttr('name');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="left_1">
<input class="magic-radio" type="radio" name="select_type" id="1" value="1">
<label for="1">1</label>
<input class="magic-radio" type="radio" name="select_type" id="2" value="2">
<label for="2">2</label>
<input class="magic-radio" type="radio" name="select_type" id="3" checked value="3">
<label for="3">3</label>
</div>
<div class="left_2 one">
<select name="type" id="abc_type_1" class="form-control abc_type">
<option value="A">LSK-A</option>
<option value="B">LSK-B</option>
<option value="C">LSK-C</option>
</select>
</div>
<div class="left_2 two">
<select name="type" id="abc_type_2" class="form-control abc_type">
<option value="AB">LSK-AB</option>
<option value="AC">LSK-AC</option>
<option value="BC">LSK-BC</option>
</select>
</div>
<div class="left_2 three">
<select name="type" id="abc_type_3" class="form-control abc_type">
<option value="super">LSK-SUPER</option>
<option value="box">BOX-K</option>
</select>
</div>
在这里我想获取所选选择框的值,但是我无法获取正确的值,请帮助我解决.
Here i want to get the value of the selected select box but i can not get the correct value, please help me to solve.
var form_data={
agent_name: $('#agent_name').val(),
number: $('#number').val(),
number_from: $('#number_from').val(),
number_to: $('#number_to').val(),
quantity: $('#quantity').val(),
amount: $('#amount').val(),
date: $('#date').val(),
commision: $('#commision').val(),
profit: $('#profit').val(),
agent_amount: $('#agent_amount').val(),
user_id: $('#user_id').val(),
type: name_type.val(),
}
推荐答案
脚本存在的问题是,您在提取要发布到服务器的值时没有处理单选按钮和下拉菜单.
Problem with script is that you are not handling radio buttons and dropdown while extracting values for posting to server.
JS
var form_data = {
agent_name: $('#agent_name').val(),
number: $('#number').val(),
number_from: $('#number_from').val(),
number_to: $('#number_to').val(),
quantity: $('#quantity').val(),
amount: $('#amount').val(),
date: $('#date').val(),
commision: $('#commision').val(),
profit: $('#profit').val(),
agent_amount: $('#agent_amount').val(),
user_id: $('#user_id').val(),
type: $("#abc_type_"+$("input[name=select_type]:checked").val()).val()
};
只需将您的form_data替换为上述脚本即可.如果不行,请通知我.
Just replace your form_data with above script. If not works let me know.
此部分通过单选按钮获取检查值.
This part getting checked value from the radio button.
$("input[name=select_type]:checked").val()
响应是这样的. 1或2或3.
response is like this. 1 or 2 or 3.
$("#abc_type_"+$("input[name=select_type]:checked").val()).val()
现在,jquery将通过定位ID来获取价值.例如#abc_type_1,#abc_type_2,#abc_type_3
Now jquery will get value by targeting the ID. eg #abc_type_1, #abc_type_2, #abc_type_3
这篇关于通过jQuery获取所需的选择框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!