从所选内容中的值获取文本
Get text from value in a selection
我试图创建一个函数,我传递一个选择器和值,我想获得文本
function getTextBySelectorAndValue(selector, value) {
return $(selector + " option[value='" + value + "']").text();
}
错误,我明白了语法错误,无法识别的表达式:[object object]option[value='1']
看起来像选择器并没有真正工作
我把这种方法称为
getTextBySelectorAndValue($("#lodgerAppointmentLocation"), $("#lodgerAppointmentLocation").val());
您正在将一个jQuery对象传递给一个需要字符串选择器的函数。
我建议将选择器字符串传递给现有函数。。。
function getTextBySelectorAndValue(selector, value) {
return $(selector + " option[value='" + value + "']").text();
}
$('#lodgerAppointmentLocation').on('change', function() {
$('div#output').text(getTextBySelectorAndValue("#lodgerAppointmentLocation", $("#lodgerAppointmentLocation").val()));
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="lodgerAppointmentLocation">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<div id="output"></div>
或者更改函数以处理jQuery对象:
function getTextBySelectorAndValue($selected, value) {
return $selected.find("option[value='" + value + "']").text();
}
$('#lodgerAppointmentLocation').on('change', function() {
$('div#output').text(getTextBySelectorAndValue($("#lodgerAppointmentLocation"), $("#lodgerAppointmentLocation").val()));
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="lodgerAppointmentLocation">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<div id="output"></div>
或者,您可以使用:selected
选择器检索所选选项的文本:
function getTextBySelectorAndValue($selected, value) {
return $selected.find("option[value='" + value + "']").text();
}
$('#lodgerAppointmentLocation').on('change', function() {
$('div#output').text($(this).find('option:selected').text());
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="lodgerAppointmentLocation">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<div id="output"></div>
编辑
我喜欢AtheistP3ace处理jQuery对象或选择器字符串的挑战
我使用instanceof jQuery
来识别每个检查对象是否是jQuery对象的jQuery对象:
function getTextBySelectorAndValue(selector, value) {
return selector instanceof jQuery ?
selector.find("option[value='" + value + "']").text() :
$(selector+" option[value='" + value + "']").text() ;
}
$('div#output').text(
getTextBySelectorAndValue("#lodgerAppointmentLocation1", 1) + " / " +
getTextBySelectorAndValue($("#lodgerAppointmentLocation2"), 2)
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="lodgerAppointmentLocation1">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select id="lodgerAppointmentLocation2">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<div id="output"></div>
相关文章:
- 需要使用javascript获取输入文本,然后将其添加到句子中
- 从ajax请求中获取原始文本
- 当运行JS函数时,如何在c#中的Edgejs中获取错误文本
- 如果所选内容在标记内,如何获取格式化文本
- 如何使用 JQuery 从相对元素获取 html 文本
- Meteor:从Twilio获取SMS文本列表,并将它们插入mongoDB
- 使用 jQuery 从选择标签中抓取文本
- jQuery 获取原始文本(未转义),以便通过下划线模板进一步解析
- 使用 jQuery 从目标页面而不是当前页面获取元素文本
- 用javascript获取svg文本值
- 如何在按子字符串更改后获取旧文本
- JavaScript:获取搜索文本在正文上的滚动位置
- 从p:inputText javascript获取值文本
- AJAX/JS:是否可以在没有提交按钮和刷新页面的情况下获取输入文本的值
- 使用JQUERY/Javascript获取DotNetNuke文本编辑器HTML值
- 使用 casperjs 抓取文本节点的最快方法
- 在Oracle Apex的javascript部分中获取富文本编辑器的HTML文本
- 如何在PHP中获取html文本框值
- Chrome扩展程序获取DOM文本并在弹出窗口中显示.html然后单击按钮
- 如何使用 ajax 实时搜索获取脚本文本框中的搜索值