I am storing multiple values in a select option and trying to get the all the values separately by splitting the value. Split seems to work as I can get the first [0] value, but I am not getting any value after that. It alerts back as undefined. Code below. Thanks.
function roi_calc(e) {
var merchant_option = $(e).prev('td').prev('td').prev('td').find('select').val();
if (merchant_option!='Merchant') {
var value_input = $(e).prev('td').prev('td').find('input').val();
if ((value_input!='') && (value_input!='Value')) {
var price_input = $(e).prev('td').find('input').val();
if ((price_input!='') && (price_input!='Price')) {
alert ('Merchant value = '+merchant_option);
//var price_arr = new Array();//tried setting array first, but same results
var price_arr = price_input.split(',',3);
//alert (price_arr);//alerts '1'
alert (price_arr[0]);//working - alerts '1'
alert (price_arr[1]);//not working - undefined
alert (price_arr[2]);//not working - undefined
}
else {alert ('Price must be entered.');}
}
else {alert ('Value must be entered.');}
}
else {alert ('Merchant must be selected');}
//alert (row_option);
}
HTML
<table style="width: 600px;">
<tr>
<td>
<select>
<option>Merchant</option>
<option value="1,2,3,4">1</option>
<option value="5,6,7,8">2</option>
</select>
</td>
<td><input value="Value"></td>
<td><input value="Price"></td>
<td onclick="roi_calc(this);"> R.O.I </td>
</tr>
</table>
on jsfiddle http://jsfiddle.net/upywN/
FOUND THE ANSWER. Stupid mistake on my part.
I was splitting price_input, but needed to be splitting merchant_option stupid mistake sorry to waste everyone's time.
this is typically invalid HTML
<option value="1,2,3,4">1</option>
see the property of option
instead you should use multiple select
like this
<select multiple="multiple">
Update: You are capturing the 'selected value' (ie. '1' not '1,2,3,4') form
<option value="1,2,3,4">1</option>
But you have to split 'text' part (ie '1,2,3,4').
You can use Javascript for grabbing the 'text' part of option like this:
var list = document.getElementById('list'); // 'list' is the id of select element
var text = list.options[list.selectedIndex].text;
//And now use your split
var price_arr = text.split(',',3);
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.