简体   繁体   中英

Pass option text instead of option value to a text field

I will pass value for multiple fields to a text field. There is a problem, select options, its value are 0 and 1, my question is how to make sure that it pass option text instead of 0 and 1? That mean pass MYR instead of 0 and SGD instead of 1?

http://jsfiddle.net/e2ScF/64/

HTML:

<select id="Salary_para1">
  <option value="">Choose...</option>
  <option value="0" selected>MYR</option>
  <option value="1" >SGD</option>
</select>
<input id="Salary_value" type="text"/>
+ <input type="checkbox" id="Salary_para2" name="Salary_para2" value=" + Allowance"  />Allowance<br/>
<input type="text" id="targetTextField" name="targetTextField" size="31" tabindex="0" maxlength="99" value="">

CODE:

$(function() {
  setTarget();
  $("#Salary_para1,#Salary_para2").change(setTarget);
  $("#Salary_value").keyup(setTarget);

  function setTarget() {
    var tmp = $("#Salary_para1").val();
    tmp += $("#Salary_value").val();
    tmp += $("#Salary_para2:checked").val() || '';
    $('#targetTextField').val(tmp);
  }
});

To get the text and not the value of whatever is selected in the dropdown, you'd have to target the selected option and get the text from that element :

var tmp = $("option:selected", "#Salary_para1").text();

FIDDLE

Is there any reason why you can't change the markup, so that option's value is equal to option's text. For example:

<option value="MYR" selected>MYR</option>

If you can't change the markup, then you can get the option's text using jQuery text method.

var tmp = $("#Salary_para1 option:selected").text();

我会这样做-

var tmp = $('option:selected', '#Salary_para1').text();

的document.getElementById( 'ID')。value.select = [位置]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM