从选择中的值中获取文本

时间:2015-12-10 20:30:33

标签: javascript jquery

我尝试创建一个函数,我传递一个选择器加上值,我想得到文本

function getTextBySelectorAndValue(selector, value) {
    return $(selector + " option[value='" + value + "']").text();
}

错误我明白了     语法错误,无法识别的表达式:[object Object]选项[value ='1']

看起来像选择器并没有真正起作用

我将此方法称为

getTextBySelectorAndValue($("#lodgerAppointmentLocation"), $("#lodgerAppointmentLocation").val());

2 个答案:

答案 0 :(得分:3)

您正在将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 selector检索所选选项的文本:

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来识别每个Check if object is a jQuery object的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>

答案 1 :(得分:1)

你没有看到我的答案而且你的问题已经更新了,所以我的答案不再合适,所以我必须删除。