我正在尝试为我的下拉菜单中的用户选择文本.
我有
var selectMenu=document.createElement('select');
selectMenu.className='menu';
for(var i=0; i<array.length; i++){
var option=document.createElement('option');
option.className='option';
option.innerHTML=array[i].name;
option.value=array[i].id;
selectMenu.appendChild(option);
}
$(selectMenu).change(function(){
//i want to get the selected text here
//I know I could get value by using $(this).val()
//but not sure how to get the selected text here.
})
Run Code Online (Sandbox Code Playgroud)
我有谷歌这个问题,我发现的都是
$('#menu option:selected).text().
Run Code Online (Sandbox Code Playgroud)
反正有没有得到我需要的东西?非常感谢!
我有一个这样的选择框:
<select id="year">
<option value="1">1 Year</option>
<option value="2">2 Years</option>
<option value="3">3 Years</option>
<option value="4">4 Years</option>
<option value="5">5 Years</option>
<option value="6">6 Years</option>
<option value="7">7 Years</option>
<option value="8">8 Years</option>
<option value="9">9 Years</option>
<option value="10">10 Years</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我想在jQuery中获得特定文本的价值,比如"4年的价值".我怎样才能做到这一点?
我有一个MVC下拉列表(Html.DropDownList),其中包含已填充的电影列表.我想在执行表单提交时检索标题(值字段),描述(文本字段).我可以访问标题(值字段),但我无法访问说明.我的代码示例如下.
//查看模型.......
公共类电影院
{
public string CinemaName { get; set; }
public SelectList MoviesList { get; set; }
public string MoviesName { get; set; }
Run Code Online (Sandbox Code Playgroud)
}
公共课电影
{
public string Title { get; set; }
public string Description { get; set; }
Run Code Online (Sandbox Code Playgroud)
}
//控制器
的[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
Cinema _cinema = GetViewModel();
ViewData.Model = _cinema;
return View();
}
public IEnumerable<Movie> GetMovieList()
{
List<Movie> list = new List<Movie>();
list.Add(new Movie(){ Title = "1", Description = "Batman" });
list.Add(new Movie() { …Run Code Online (Sandbox Code Playgroud) 使用jQuery,使用文本值在下拉列表中选择项目的最简单方法是什么.
例如,我有一个包含状态列表的下拉列表.我的文本值为"PA".我想让"PA"成为选定的值.使用jQuery执行此操作的最佳方法是什么?我一直在谷歌搜索几个小时,我找不到这个问题的例子.
请注意,下拉列表中的每个项目都有一个数字键.我知道$(#ddlStates).val(key)将选择我想要的值.但是,我没有关键只有测试("PA")
谢谢你的帮助.
使用最新的 select2 和多个选项。当用户选择任何选项时,我需要创建所选选项值和文本的数组。为了获得价值,我使用:
var id=$('#select2_dropdown').val();
Run Code Online (Sandbox Code Playgroud)
它使数组像id[]="value1", id[]="value2",等等。
现在我需要为标签之间的文本创建相同的数组<option>。在使用 jQuery 从下拉列表(选择框)获取选定文本中找到了解决方案,因此添加了以下代码:
var name=$("#select2_dropdown option:selected").text();
Run Code Online (Sandbox Code Playgroud)
虽然它非常适合单选,但尝试将它与多选一起使用只会产生一个类似的字符串name=optiontext1optiontext2(所有选定选项的字符串合并为一个)
我怎样才能让它看起来像这样:name[value1]=optiontext1, name[value2]=optiontext2?
尝试过这个但没有任何运气:
var id=$("#select2_dropdown option:selected").text();
var name[id]=$("#select2_dropdown option:selected").text();
Run Code Online (Sandbox Code Playgroud) 所以这是 HTML 代码:
<select id='selUser' style='width: 200px;'>
<option value='0'>Select User</option>
<option value='1'>Yogesh singh</option>
<option value='2'>Sonarika Bhadoria</option>
<option value='3'>Anil Singh</option>
<option value='4'>Vishal Sahu</option>
<option value='5'>Mayank Patidar</option>
<option value='6'>Vijay Mourya</option>
<option value='7'>Rakesh sahu</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我想要实现的是,当我单击值“1”时,下拉列表将显示“1”而不是 Yogesh Singh。有可能这样做吗?
我有这个HTML:
<select class="business_types_select" name="business_type_id">
<option value="0" selected="selected">Select a Business Type</option>
<option value="54">Bakery</option>
<option value="55">Tobacco</option>
</select>
Run Code Online (Sandbox Code Playgroud)
如果我选择面包店然后尝试:
$(".business_types_select").val()
Run Code Online (Sandbox Code Playgroud)
我得到54,但我怎么得到文本面包店?如果我试试
$(".business_types_select").text()
Run Code Online (Sandbox Code Playgroud)
我明白了:
" Select a Business Type Bakery Tobacco "
Run Code Online (Sandbox Code Playgroud) 应该发生什么: 每次单击列表中的项目(单个选择)时,该对象中的信息将显示在单独的div上.
问题: 我无法将信息显示在单独的div上.
包含列表和预览区域的表应显示:
<table id="preview" style="width: 100%;" class="border">
<tr>
<td style="width: 25%;">
<!-- List here -->
</td>
<td style="width: 75%;">
<div id="template_preview" style="overflow:auto">
</div>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
加载时,将调用:
<script type="text/javascript" language="javascript">
$(document).ready(function(){
changePreview();
});
</script>
Run Code Online (Sandbox Code Playgroud)
该 changePreview() 函数:
function changePreview()
{
var selectedValueFromListId = $('#listId').val();
$('#template_preview').html('<c:forEach var="some_var" items="${model.someList}">');
$('#template_preview').append('<input type="hidden" id="someTemplateId" value="${some_var.someListId}" />');
var someTemplateId = $('#someTemplateId').val();
if (selectedValueFromListId == someTemplateId)
{
$('#template_preview').append('test');
$('#template_preview').append('<c:if test="${some_var.objectOne.objectTwo.objectTwoId == valueOfSomething}">');
$('#template_preview').append('some text here - <c:out value="${some_var.label}" />');
$('#template_preview').append('</c:if>');
}
$('#template_preview').append('</c:forEach>');
} …Run Code Online (Sandbox Code Playgroud) 如何从下拉列表中获取选定的文本?在因为要求重复而折磨我之前,我已阅读: 使用 jQuery 从下拉列表(选择框)获取选定的文本和 使用 jQuery 从下拉列表(选择框)获取选定的文本,并尝试了以下代码变体这些页面:
<asp:DropDownList ID="DDLSuburb" runat="server">
</asp:DropDownList>
alert($get("[id*='DDLsuburb'] :selected"));
alert($("[id*='DDLsuburb'] :selected"));
alert($get("#DDLsuburb option:selected"));
alert($get("DDLsuburb option:selected"));
alert($get("#DDLsuburb :selected").text());
alert($get("DDLsuburb :selected").text());
alert($get("DDLSuburb", Text));
alert($get(DDLSuburb, Text).toString());
alert($get("DDLSuburb", Text).toString());
alert($get("DDLSuburb").html());
alert($get("DDLSuburb :selected").html());
alert($get("DDLSuburb option:selected").html());
alert($get(DDLSuburb).textContent());
alert($get(DDLSuburb).innerHTML());
alert($get(DDLSuburb).innerHTML.toString());
alert($get("DDLSuburb").text());
alert($get("DDLSuburb").valueOf("DDLSuburb"));
alert($get("DDLSuburb").valueOf());
Run Code Online (Sandbox Code Playgroud)
注意: 1. 我使用警报进行故障排除。2.我知道第一部分应该是($get("DDLSuburb"),而不是不带引号的选项。Visual Studio 2015,ASP.net。
编辑:尝试通过单击按钮引发警报:
<input type="button" value="Get Postcode" onclick="onClick()" />
<script type="text/javascript">
var onClick = function () {
alert($get("DDLSuburb")...);
}
Run Code Online (Sandbox Code Playgroud) id当它被选中时,我一直在寻找一种从下拉列表中获取选项的方法.我找到了以下内容:
使用jQuery从下拉列表(选择框)中获取所选文本
并尝试将接受的答案更改为:
$("#yourdropdownid option:selected").id;
Run Code Online (Sandbox Code Playgroud)
但是当我alert()这样做时,它给了我"未定义".有没有办法使用JQuery获取id?