I tried to write codes of to display value in texbox but that value is displed based on selected value from combox that works fine,
But what I want now is if I select ItemCode from combobox I need ItemPrice to be displayed in the textbox
What I have now is if I select Item the value which is between <option>**Item Name**</option>
is displayed in that textbox instead ItemPrice
Below is my sample codes which runs fine
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" /></script>
<span id="spanSegmentData">
<?php
require_once('connection.php');
$queItem = mysqli_query($connect, "select * from oitm");
echo "<select name="segment_id" class="form-control" id="segment_id" STYLE="width: 300px" >";
echo "<option value="" selected="selected">Select a product</option>";
while ($rowStockIn = mysqli_fetch_array($queItem)) {
$ItemCode2 = $rowStockIn['ItemCode'];
$ItemName2 = $rowStockIn['ItemName'];
$ItemPrice2 = $rowStockIn['ItemPrice'];
echo "<option value="$ItemCode2">$ItemCode2 $ItemName2</option>";
}
echo "</select>";
?>
</span>
<span id="dt_title"> <input id="title" name="title" type="text" value="<?php echo "$ItemPrice2";?>" data-title="MB" style="width:300px" readonly> </span>
<script>
$("#segment_id").change(function () {
var id = $(this).val();
$("#title").val($("#segment_id option:selected").text());
});
</script>
Sample of my form
I want to allow all rows to display value in textbox based on value selected in combobox
Please anyone can help me to display that ItemPrice in the textbox
2
Answers
1-For multiple row use class instead of id. add class ‘segment_id’ in select box and ‘title’ in text box which is use to display the price in row.
2- Add the ‘itemprice’ value in option with data attribute itemprice.
3- In jQuery on change get the selected data attr value and add in text box of closest row.
like.
In jQuery get the data attr value and add in text box of closest row.
like
DEMO
send send also $ItemPrice2 form value with –.
in script :