问题
How to get the price of the related product, when I select the product from selectbox?
I am fetching product names and prices from database (Laravel Mysql project). I want to display price on price text box. Following is my view`
<form role="form">
<div class="form-group">
<label for="Vehicle"> Vehicle Name:</label>
<select class="form-control">
@foreach($servicevehicles as $servicevehicle )
<option>{{$servicevehicle->vehiclename}}</option>
<!--here dispaly vehicle name-->
@endforeach
</select>
</div>
<div class="form-group">
<label for="price"> Price</label>
<!--i want to display related product when i select related vehicle from above selectbox-->
<input type="text" class="form-control" >
</div>
</form>
回答1:
I would recommend you to print the price as data-attribute in the option tag like this:
$('.vehicle-type').on('change', function() {
$('.price-input')
.val(
$(this).find(':selected').data('price')
);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form role="form">
<div class="form-group vehicle-type">
<label for="Vehicle"> Vehicle Name:</label>
<select class="form-control">
<option data-price="345">Title 1</option>
<option data-price="122">Title 2</option>
</select>
</div>
<div class="form-group">
<label for="price"> Price</label>
<input type="text" class="form-control price-input" readonly>
</div>
</form>
Then with JS (jQuery) listen to the Change event to extract new value from the selected option. This should work.
回答2:
Maybe something like this
$("#your_option").change(function() {
var get_text = $("#your_option option:selected").text();
//Do something with the get_text
});
This will get the text of the option thats selected fiddle about with it and apply it for your code
回答3:
use foreach like this:
@foreach($servicevehicles as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
In your controller define the servicevehicles
$servicevehicles = Model::all()->pluck('name','price');
And use this script.
<form>
<select id="selectid00">
<option value="samsung">Samsung</option>
<option value="iphone">Iphone</option>
<option value="generalmobile">GM</option>
<option value="sony">Sony</option>
</select>
</form>
<button type="button" onclick="f01()">f01</button>
<p id="p00">TEXT</p>
<script>
function f01() {
var x = document.getElementById("selectid00").value;
document.getElementById("p00").innerHTML = x;
}
</script>
回答4:
Try this simple code:
$('#select_car').on('change', function() {
$('.input_price').val($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form role="form">
<div class="form-group ">
<label for="Vehicle"> Vehicle Name:</label>
<select id="select_car" class="form-control">
<option value="10000">Honda</option>
<option value="2000">Toyota</option>
<option value="100000">Ford</option>
</select>
</div>
<div class="form-group">
<label for="price"> Price</label>
<input type="text" class="form-control input_price" readonly>
</div>
</form>
来源:https://stackoverflow.com/questions/48437891/how-to-get-price-of-related-product-when-i-select-product-from-a-selectbox