jQuery : Select 所有 Select 元素选择值仅发送 ajax
jQuery : Select all Select Element with value selected only to send in ajax
我正在尝试提高我的 ajax 请求速度所以我的 ajax 代码是这样的
$.ajax({
url:url_option,
type:'POST',
dataType:'json',
data:$('#product input[type=\'text\'], #product input[type=\'number\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\'], #product select, #product textarea, #product input[name=\'quantity\']'),
success:function(json){
},
});
因为我这样使用它 $('#product select')
它会拾取所有 select 框 有什么方法可以只拾取已经 selected 的 select 元素以便我只能在 ajax 请求
中发送 selected select 元素
我试过很多方法,比如使用 has()
、not()
和使用伪但无法找到一种方法来只选择 selected Select 元素修改这个:$('#product select')
例如。
我在这个代码笔中有三个 select 框 2 默认 selected 和 1 unselected 当我 console.log($('#product select'));
所有输入里面 div 它 returns 所有三个 2 selected 和 1 unselected 但我只需要 select 两个 Select 元素,即 selected
console.log($('#product select')); // is there any way to pick only 2 selected select element
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="product">
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28521">HDD/SSD OPTION:</label>
<select name="option[28521]" id="input-option28521" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192907" selected="selected">250GB HDD</option>
<option value="192909">320GB HDD</option>
<option value="192910">500GB HDD</option>
<option value="192911">750GB HDD</option>
<option value="192912">1TB HDD</option>
</select>
</div>
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28523">Ram:</label>
<select name="option[28523]" id="input-option28523" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192946">4GB</option>
<option value="192948">6GB</option>
<option value="192949">8GB</option>
<option value="192950">10GB</option>
</select>
</div>
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28527">Add Antivirus:</label>
<select name="option[28527]" id="input-option28527" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192968" selected="selected">Leave Me Unprotected</option>
<option value="192970">Install QuickHeal Antivirus 1-Year License (+₦2,500) Renewable</option>
</select>
</div>
</div>
如果有人知道或有任何方法已经在 SE 中 posted,请指出 post
谢谢
您可以使用.filter()
,对return那些有值的选择:
$('#product select').filter(function(){ return $(this).val().length })
$(() => {
console.log($('#product select').filter(function(){ return $(this).val().length }).length)
})
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="product">
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28521">HDD/SSD OPTION:</label>
<select name="option[28521]" id="input-option28521" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192907" selected="selected">250GB HDD</option>
<option value="192909">320GB HDD</option>
<option value="192910">500GB HDD</option>
<option value="192911">750GB HDD</option>
<option value="192912">1TB HDD</option>
</select>
</div>
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28523">Ram:</label>
<select name="option[28523]" id="input-option28523" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192946">4GB</option>
<option value="192948">6GB</option>
<option value="192949">8GB</option>
<option value="192950">10GB</option>
</select>
</div>
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28527">Add Antivirus:</label>
<select name="option[28527]" id="input-option28527" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192968" selected="selected">Leave Me Unprotected</option>
<option value="192970">Install QuickHeal Antivirus 1-Year License (+₦2,500) Renewable</option>
</select>
</div>
</div>
尝试 jquery :selected 选择器 $('#product option:selected')
。您可以遍历它们以访问值。
$('#product option:selected').each(function() {
console.log($(this).val());
});
参考 jquery :selected selector 文档。
我正在尝试提高我的 ajax 请求速度所以我的 ajax 代码是这样的
$.ajax({
url:url_option,
type:'POST',
dataType:'json',
data:$('#product input[type=\'text\'], #product input[type=\'number\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\'], #product select, #product textarea, #product input[name=\'quantity\']'),
success:function(json){
},
});
因为我这样使用它 $('#product select')
它会拾取所有 select 框 有什么方法可以只拾取已经 selected 的 select 元素以便我只能在 ajax 请求
我试过很多方法,比如使用 has()
、not()
和使用伪但无法找到一种方法来只选择 selected Select 元素修改这个:$('#product select')
例如。
我在这个代码笔中有三个 select 框 2 默认 selected 和 1 unselected 当我 console.log($('#product select'));
所有输入里面 div 它 returns 所有三个 2 selected 和 1 unselected 但我只需要 select 两个 Select 元素,即 selected
console.log($('#product select')); // is there any way to pick only 2 selected select element
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="product">
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28521">HDD/SSD OPTION:</label>
<select name="option[28521]" id="input-option28521" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192907" selected="selected">250GB HDD</option>
<option value="192909">320GB HDD</option>
<option value="192910">500GB HDD</option>
<option value="192911">750GB HDD</option>
<option value="192912">1TB HDD</option>
</select>
</div>
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28523">Ram:</label>
<select name="option[28523]" id="input-option28523" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192946">4GB</option>
<option value="192948">6GB</option>
<option value="192949">8GB</option>
<option value="192950">10GB</option>
</select>
</div>
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28527">Add Antivirus:</label>
<select name="option[28527]" id="input-option28527" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192968" selected="selected">Leave Me Unprotected</option>
<option value="192970">Install QuickHeal Antivirus 1-Year License (+₦2,500) Renewable</option>
</select>
</div>
</div>
如果有人知道或有任何方法已经在 SE 中 posted,请指出 post 谢谢
您可以使用.filter()
,对return那些有值的选择:
$('#product select').filter(function(){ return $(this).val().length })
$(() => {
console.log($('#product select').filter(function(){ return $(this).val().length }).length)
})
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="product">
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28521">HDD/SSD OPTION:</label>
<select name="option[28521]" id="input-option28521" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192907" selected="selected">250GB HDD</option>
<option value="192909">320GB HDD</option>
<option value="192910">500GB HDD</option>
<option value="192911">750GB HDD</option>
<option value="192912">1TB HDD</option>
</select>
</div>
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28523">Ram:</label>
<select name="option[28523]" id="input-option28523" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192946">4GB</option>
<option value="192948">6GB</option>
<option value="192949">8GB</option>
<option value="192950">10GB</option>
</select>
</div>
<div class="form-select-group form-group required">
<label class="control-label" for="input-option28527">Add Antivirus:</label>
<select name="option[28527]" id="input-option28527" class="form-control">
<option value=""> --- Please Select --- </option>
<option value="192968" selected="selected">Leave Me Unprotected</option>
<option value="192970">Install QuickHeal Antivirus 1-Year License (+₦2,500) Renewable</option>
</select>
</div>
</div>
尝试 jquery :selected 选择器 $('#product option:selected')
。您可以遍历它们以访问值。
$('#product option:selected').each(function() {
console.log($(this).val());
});
参考 jquery :selected selector 文档。