Reputation: 49
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?
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);
}
});
Upvotes: 0
Views: 1877
Reputation: 6000
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();
Upvotes: 2
Reputation: 34416
I would do this -
var tmp = $('option:selected', '#Salary_para1').text();
Upvotes: 0