如何使用jQuery在selectbox中选择一个值? [英] How to select a value in a selectbox with jQuery?
本文介绍了如何使用jQuery在selectbox中选择一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个多重下拉框和一个输入如下:
< select name =offer_typemultiple =多个>
< option value =1>热卖优惠< / option>
< option value =2>最佳优惠< / option>
< option value =3> Special / Festival Offer< / option>
< option value =4> Side Offer< / option>
< option value =5>热门优惠< / option>
< option value =6> Megha classified Offers< / option>
< option value =7> Buy One / Get One Offer< / option>
< option value =8> Inagural Offer< / option>
< / select>
< input type =textname =offervalue =/>
现在,我想在优惠中选择 offer_type / strong>无页面刷新。与Jquery。
解决方案
这应该为你做,你不需要任何PHP,只是jQuery:
$(document).ready(function(){
$('select [name =offer_type]' (function(){
var selectedValue = $(this).find('option:selected')。val();
$('input [name =offer]' selectedValue);
});
});
对于多重选择,请尝试以下操作:
($($)$($)$($)$($)$($) $ b var selectedValsAsString ='';
$(this).find('option:selected')。each(function(){
selectedValsAsString + = $(this).val()+' ;
});
$('input [name =offer]')。val(selectedValsAsString);
});
});
这将显示您在文本字段中以空格分隔的答案。
I have a multiselect dropdown box and a input as below:
<select name="offer_type" multiple="multiple">
<option value="1">Hot Offer</option>
<option value="2">Best Offer</option>
<option value="3">Special / Festival Offer</option>
<option value="4">Side Offer</option>
<option value="5">Top Offer</option>
<option value="6">Megha classified Offers</option>
<option value="7">Buy One / Get One Offer</option>
<option value="8">Inagural Offer</option>
</select>
<input type="text" name="offer" value="" />
Now I want the selected value of offer_type in offer without page refresh. with Jquery.
解决方案
This should do it for you, you don't need any PHP, just jQuery:
$(document).ready(function() {
$('select[name="offer_type"]').change(function() {
var selectedValue = $(this).find('option:selected').val();
$('input[name="offer"]').val(selectedValue);
});
});
For the multiple select, try something like this:
$(document).ready(function() {
$('select[name="offer_type"]').click(function() {
var selectedValsAsString = '';
$(this).find('option:selected').each(function() {
selectedValsAsString += $(this).val()+' ';
});
$('input[name="offer"]').val(selectedValsAsString);
});
});
This will display your answers in the text field separated by a space.
这篇关于如何使用jQuery在selectbox中选择一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文