Jquery 使用多个下拉列表中的选定值
我的页面中有几个下拉列表来收集用户偏好。我需要从每个下拉列表中获取用户选择的值,然后将它们传递给本地asp程序使用。 我使用了:Jquery 使用多个下拉列表中的选定值,jquery,select,Jquery,Select,我的页面中有几个下拉列表来收集用户偏好。我需要从每个下拉列表中获取用户选择的值,然后将它们传递给本地asp程序使用。 我使用了: $(document).ready(function() { $('#my_select').change(function() { // assign the value to a variable var selectVal = $('#my_select :select
$(document).ready(function() {
$('#my_select').change(function() {
// assign the value to a variable
var selectVal = $('#my_select :selected').val();
$(".test").html(selectVal);
});
});
它从一个下拉列表中获取值,但我似乎无法在我的页面后面使用该值。
到目前为止,我所能找到的只是如何使用一个下拉列表中的值。看起来您为多个选择框(“我的选择”)设置了一个ID-这一点从一开始就不正确。如果您有多个选择,请为每个选择指定一个唯一的ID或(更好)一个样式(如.my\u select)。然后,您可以按如下方式修改js:
$('.my_select').change(function() {
// assign the value to a variable
var selectVal = $("option:selected", this);
$(".test").html(selectVal);
});
看起来您为多个选择框(#my_select)设置了一个ID,这一点从一开始就不正确。如果您有多个选择,请为每个选择指定一个唯一的ID或(更好)一个样式(如.my\u select)。然后,您可以按如下方式修改js:
$('.my_select').change(function() {
// assign the value to a variable
var selectVal = $("option:selected", this);
$(".test").html(selectVal);
});
使用此代码:
$('#my_select').change(function () {
$(".test").html("");
$('#my_select :selected').each(function (i, selected) {
// assign the value to a variable
var selectVal = $(this).val();
$(".test").append(selectVal);
});
});
使用此代码:
$('#my_select').change(function () {
$(".test").html("");
$('#my_select :selected').each(function (i, selected) {
// assign the value to a variable
var selectVal = $(this).val();
$(".test").append(selectVal);
});
});
这是要发送到服务器的响应:
[{“name”:“s1”、“val”:“v1”}、{“name”:“s2”、“val”:“v4”}、{“name”:“s3”、“val”:“v6”}]
这是要发送到服务器的响应:
[{“name”:“s1”、“val”:“v1”}、{“name”:“s2”、“val”:“v4”}、{“name”:“s3”、“val”:“v6”}]
要在使用:selected选择元素时获得最佳性能,请首先使用纯CSS选择器选择元素,然后使用.filter(“:selected”)
要在使用:selected选择元素时获得最佳性能,请首先使用纯CSS选择器选择元素,然后使用.filter(“:selected”)
这很简单
html代码:
<select class="select_field" id="l_type" name="l_type">
<option value=" ">Select type</option>
<option value="cu">Cu</option>
<option value="Ve">Ve</option>
<option value="Ex">Ex</option>
</select>
<select class="select_field" id="l_type" name="l_type">
<option value=" ">Select type</option>
<option value="cu">Cu</option>
<option value="Ve">Ve</option>
<option value="Ex">Ex</option>
</select>
<select class="select_field" id="l_type" name="l_type">
<option value=" ">Select type</option>
<option value="cu">Cu</option>
<option value="Ve">Ve</option>
<option value="Ex">Ex</option>
</select>
<input type="button" value="GetVal"/>
$(document).ready(function () {
var selectVal = [];
$('input:button').click(function () {
$('select').each(function(i,e){
selectVal.push($('select:eq('+i+') option:selected').val());
});
console.log(selectVal);
});
});
最后,非常简单
html代码:
<select class="select_field" id="l_type" name="l_type">
<option value=" ">Select type</option>
<option value="cu">Cu</option>
<option value="Ve">Ve</option>
<option value="Ex">Ex</option>
</select>
<select class="select_field" id="l_type" name="l_type">
<option value=" ">Select type</option>
<option value="cu">Cu</option>
<option value="Ve">Ve</option>
<option value="Ex">Ex</option>
</select>
<select class="select_field" id="l_type" name="l_type">
<option value=" ">Select type</option>
<option value="cu">Cu</option>
<option value="Ve">Ve</option>
<option value="Ex">Ex</option>
</select>
<input type="button" value="GetVal"/>
$(document).ready(function () {
var selectVal = [];
$('input:button').click(function () {
$('select').each(function(i,e){
selectVal.push($('select:eq('+i+') option:selected').val());
});
console.log(selectVal);
});
});
最后,这里有一个演示给你。看看
HTML:
<select name="select_box_1" id="select_box_1" class="sel_boxes">
<option value="select_box_1_value_1">select_box_1_value_1</option>
<option value="select_box_1_value_2">select_box_1_value_2</option>
</select>
<select name="select_box_2" id="select_box_2" class="sel_boxes">
<option value="select_box_2_value_1">select_box_2_value_1</option>
<option value="select_box_2_value_2">select_box_2_value_2</option>
</select>
<select name="select_box_3" id="select_box_3" class="sel_boxes">
<option value="select_box_3_value_1">select_box_3_value_1</option>
<option value="select_box_3_value_2">select_box_3_value_2</option>
</select>
这里有一个演示给你。看看
HTML:
<select name="select_box_1" id="select_box_1" class="sel_boxes">
<option value="select_box_1_value_1">select_box_1_value_1</option>
<option value="select_box_1_value_2">select_box_1_value_2</option>
</select>
<select name="select_box_2" id="select_box_2" class="sel_boxes">
<option value="select_box_2_value_1">select_box_2_value_1</option>
<option value="select_box_2_value_2">select_box_2_value_2</option>
</select>
<select name="select_box_3" id="select_box_3" class="sel_boxes">
<option value="select_box_3_value_1">select_box_3_value_1</option>
<option value="select_box_3_value_2">select_box_3_value_2</option>
</select>
获取所有选定值非常容易,如下所示:
$('select').each(function(){
console.log($(this).attr("name") = $(this).val());
});
我已经做了我认为是OP需要传递给ASP的:
var vals = [], str=null;
$('select').change(function(){
getVals();
});
$('button').click(function(){
getVals();
printVals();
});
function getVals(){
vals = [];
$('select').each(function(){
vals[$(this).attr("name")] = $(this).val();
});
};
function printVals(){
str="";
for (var key in vals) {
str += ((str)?"&":"") + key + "=" + vals[key];
}
alert(str);
}
获取所有选定值非常容易,如下所示:
$('select').each(function(){
console.log($(this).attr("name") = $(this).val());
});
我已经做了我认为是OP需要传递给ASP的:
var vals = [], str=null;
$('select').change(function(){
getVals();
});
$('button').click(function(){
getVals();
printVals();
});
function getVals(){
vals = [];
$('select').each(function(){
vals[$(this).attr("name")] = $(this).val();
});
};
function printVals(){
str="";
for (var key in vals) {
str += ((str)?"&":"") + key + "=" + vals[key];
}
alert(str);
}
u应将变量声明为全局变量以访问其数据..u应将变量声明为全局变量以访问其数据..无需:选中。您可以测试其价值,无需选择。你可以测试它的有效性,我想这可能有用。但是$(“.test”).html(selectVal);仍然只在change函数中起作用。我想这可能会起作用。但是$(“.test”).html(selectVal);仍然只在change函数内部工作。非常感谢。这似乎就是我的票。非常感谢。这似乎就是那张票。