2
Three things I see here:
- You’re setting the value incorrectly. Instead of
.prop('value') = 'Long'
, which isn’t valid JavaScript, just use.val('Long')
. - You’re re-using the “result” ID, so you’re targeting the wrong element. IDs must be unique.
- The HTML is incomplete. Might not be a cause of the problem, but certainly worth fixing.
So something more like this:
var value = $('#value');
$(value).change(function() {
debugger;
if (Number($(this).val()) > 60) {
$('#result').val('Long'); // 1. Setting the value correctly with jQuery
}
if (Number($(this).val()) < 60) {
$('#result').val('Short');
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="value" class="col-form-label col-sm-4">Value</label>
<div class="col-sm-7"> <input type="number" name="value" class="numberinput form-control" id="value"> </div>
</div>
<div><i>Result:</i></div>
<div class="form-group row"> <!--- 2. Removing the errant ID --->
<div class="col-sm-12">
<select name="result" class="select2 form-control" style="width: 100%;" id="result">
<option value="Long">Long</option>
<option value="Short">Short</option>
</select> <!--- 3. Correcting the remaining HTML --->
</div>
</div>
5
You can set the selected value using .val(....)
.
var value = $('#value');
$(value).change(function() {
debugger;
if (Number($(this).val()) > 60) {
$('select').val("Long");
}
if (Number($(this).val()) < 60) {
$('select').val("Short");
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="value" class="col-form-label col-sm-4">Value</label>
<div class="col-sm-7"> <input type="number" name="value" class="numberinput form-control" id="value"> </div>
</div>
<div><i>Result:</i></div>
<div id="result" class="form-group row">
<div class="col-sm-12">
<select name="result" class="select2 form-control" style="width: 100%;" id="result">
<option value="Long">Long</option>
<option value="Short">Short</option>
Beside that, you have some problems in the HTML, you’ve used the same ID (result
) multiple times. IDs are meant to be unique.
As @Archer mentioned, none of the two current conditions will match if the value is 60
, you will need to do a >=
or <=
comparison to account for those cases.
- [Django]-ElasticSearch term suggest on analyzed field returns no suggestions
- [Django]-Get ID of Django record after it has been created in a ModelViewSet
- [Django]-POSTing foreign keys to Django Rest Framework, using Postman
- [Django]-Django– Deactivate User account instead of deleting it
1
// No need to wrap '#value' twice with jQuery function
$('#value').change(function() {
// You do not need two ifs, since the two value are exclusive
const option = $(this).val() > 60 ? "Long" : "Short";
// You had two different elements with the id "result"
// and .val is a function, you can pass the new value as argument
$('#selection').val(option);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="value" class="col-form-label col-sm-4">Value</label>
<div class="col-sm-7"> <input type="number" name="value" class="numberinput form-control" id="value" /> </div>
</div>
<div><i>Result:</i></div>
<div id="result" class="form-group row">
<div class="col-sm-12">
<select name="result" class="select2 form-control" style="width: 100%;" id="selection">
<option value="Short">Short</option>
<option value="Long">Long</option>
</select>
</div>
</div>
- [Django]-How do you fix the following Django Error: "Type: IOError" "Value: [Errno 13] Permission denied"
- [Django]-Django extend admin "index" view
- [Django]-How to get access to context from Jinja2 extension
- [Django]-JQuery and django-rest-framework-bulk: send list
- [Django]-How to tag whole ViewSet in drf_yasg?
0
i think,it will help you
<script src="Scripts/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function () {
$("#value").on('input', function () {
var value = parseInt($("#value").val());
if (value > 60) {
$("#result").prop("value", "Long");
}
if (value <= 60) {
$("#result").prop("value", "Short");
}
});
});
</script>
<input type="number" name="value" class="numberinput form-control" id="value" />
<select name="result" class="select2 form-control" style="width: 100%;" id="result">
<option value="Short">Short</option> <option value="Long">Long</option>
</select>
- [Django]-How to make a geography field unique?
- [Django]-PostgreSQL on AWS ECS: psycopg2.OperationalError invalid port number 5432
- [Django]-{{ STATIC_URL }} with pyjade in django
- [Django]-Check if a non-nullable field is null
- [Django]-Postfix hangs when sending email
Source:stackexchange.com