答案 0 :(得分:1)
您可以通过在select
input
$(function() {
const selectListEle = $('select');
selectListEle.on('input', function(e) {
const selectVal = selectListEle.val();
const inputVal = $('input').val();
if (selectVal === 'withdrawal') {
if (inputVal) {
$('input').val(-inputVal);
}
} else {
if (inputVal) {
$('input').val(-inputVal);
}
}
});
});
事件来实现
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>none</option>
<option>withdrawal</option>
</select>
<input type="number" value="">
$.ajax({
url: "http://localhost:5984/_session",
type: "POST",
data: {
username: "myUsername",
password: "myPassword",
},
success: function (result) {
console.log(result);
},
error: function (err) {
console.log(err);
},
});
答案 1 :(得分:0)
在整个DOM中搜索$('input')是一个糟糕的主意。
更清洁:
$(function() {
$('.selectA').on('input', function(e) {
var selectedVal = $(e.currentTarget).val(),
$targetInput = $('.inputA'),
inputVal = $targetInput.val();
if (selectedVal === 'withdrawal' && inputVal.length) {
$targetInput.val(['-', inputVal].join(''));
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="selectA">
<option>none</option>
<option>withdrawal</option>
</select>
<input class="inputA" type="number" value="">
答案 2 :(得分:0)
一个选择是为select
赋予option
1
值为+ve
,为-1
赋予-ve
,那么您只需多个这两个值在一起。
$("#inp").val() * $("#opt").val()
function calc() {
$(this).nextAll("#result").text($("#inp").val() * $("#opt").val());
}
$("#inp").on("input", calc);
$("#opt").on("change", calc);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="opt">
<option value="1">deposit</option>
<option value="-1">withdrawal</option>
</select>
<input id="inp" type="number" value="" />
<span id='result'></span>