CodeIgniter form_dropdown选择值 [英] CodeIgniter form_dropdown selected value
问题描述
我有一个表单使用CI的form_dropdown从DB ...拉出一个数组。
$ osValue ='id = osValue';
echo form_dropdown('os',$ os,'',$ osValue);
当检查元素时,在用户代理中如下:
< select name =osos>
< option value =1> Windows XP 32位< / option>
< option value =2> Windows XP 64位< / option>
< option value =3> Windows Vista 32位< / option>
< option value =4> Windows Vista 64位< / option>
< option value =5> Windows 7 32位< / option>
< option value =6> Windows 7 64位< / option>
< option value =7> Server 2003 32位< / option>
< option value =8> Server 2003 64位< / option>
< option value =9> Server 2008 32位< / option>
< option value =10> Server 2008 64位< / option>
< option value =11> Linux< / option>
< / select>
我遇到的麻烦是只能将SELECTED值传递给变量。 / p>
当使用
时,我得到所有选项返回。 var os = $ ('#osValue')。html();
或
var os = $('#osValue')。text();
而且,在使用时,只返回值(由数据库传入的数字字符串) p>
var os = $('#osValue')。val();
必须有一个或两个我缺少为了只有选择返回值。任何/所有的建议或帮助将非常感激。提前感谢!
您有几个问题:
1)选择没有ID,但你正在查询id(你有一个out-of-placeos在那里; < select name =osos> ;
无效)
2)您不是在寻找已选择的选项
要修复,请选择 id
:
< select name =osid =os>
< option value =1> Windows XP 32位< / option>
< option value =2> Windows XP 64位< / option>
< option value =3> Windows Vista 32位< / option>
< option value =4> Windows Vista 64位< / option>
< option value =5> Windows 7 32位< / option>
< option value =6> Windows 7 64位< / option>
< option value =7> Server 2003 32位< / option>
< option value =8> Server 2003 64位< / option>
< option value =9> Server 2008 32位< / option>
< option value =10> Server 2008 64位< / option>
< option value =11> Linux< / option>
< / select>
并从jQuery中查询 :
var osval = $('#os option:selected')
或,按名称查询:
var osval = $('[name =os ] option:selected')
I have a form using CI's form_dropdown that pulls an array from DB....
$osValue = 'id = "osValue"';
echo form_dropdown('os', $os, '', $osValue);
which, when inspecting the element, is as follows in the user agent...
<select name="os" os>
<option value="1">Windows XP 32-bit</option>
<option value="2">Windows XP 64-bit</option>
<option value="3">Windows Vista 32-bit</option>
<option value="4">Windows Vista 64-bit</option>
<option value="5">Windows 7 32-bit</option>
<option value="6">Windows 7 64-bit</option>
<option value="7">Server 2003 32-bit</option>
<option value="8">Server 2003 64-bit</option>
<option value="9">Server 2008 32-bit</option>
<option value="10">Server 2008 64-bit</option>
<option value="11">Linux</option>
</select>
What I'm having trouble with is being able to pass ONLY the SELECTED value into a variable.
I get ALL options returned, when using
var os = $('#osValue').html();
or
var os = $('#osValue').text();
And, only the value (a numerical string passed in by the DB) is returned when using
var os = $('#osValue').val();
There has to be a small piece or two that I'm missing in order to have only the selected value returned. Any/all advice or help would greatly appreciated. Thank you in advance!
You have a couple of problems:
1) There is no ID on the select, yet you are querying for the id (you have an out-of-place "os" in there too; <select name="os" os>
is not valid)
2) You aren't looking for which option has been selected
To fix, give the select an id
:
<select name="os" id="os">
<option value="1">Windows XP 32-bit</option>
<option value="2">Windows XP 64-bit</option>
<option value="3">Windows Vista 32-bit</option>
<option value="4">Windows Vista 64-bit</option>
<option value="5">Windows 7 32-bit</option>
<option value="6">Windows 7 64-bit</option>
<option value="7">Server 2003 32-bit</option>
<option value="8">Server 2003 64-bit</option>
<option value="9">Server 2008 32-bit</option>
<option value="10">Server 2008 64-bit</option>
<option value="11">Linux</option>
</select>
and query that from jQuery:
var osval = $('#os option:selected')
or, query by name:
var osval = $('[name="os"] option:selected')
这篇关于CodeIgniter form_dropdown选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!