我有这个脚本
<script>
function trigger(){
var x = document.getElementById('xcoord');
var y = document.getElementById('ycoord');
var box = document.getElementById('touch');
if (x.value >= 325 || x.value <= 300 && y.value >= 55 || y.value <= 25) {
$('#touch').value('passed');
}
}
</script>
这是html
<input onchange="trigger()" required type="number" id="xcoord" name="xcoord">
<input onchange="trigger()" required type="number" id="ycoord" name="ycoord">
<input required type="text" id="touch" name="touch">
每当xcoord和ycoord的值适合条件时,我都需要显示一个值,但即使条件为真,也不会打印任何值。
答案 0 :(得分:4)
您将JavaScript
与JQuery
混合在一起。
使用JavaScript
将值放入输入框box.value = 'passed';
CodeSnippet
function trigger(){
var x = document.getElementById('xcoord');
var y = document.getElementById('ycoord');
var box = document.getElementById('touch');
if (x.value >= 325 || x.value <= 300 && y.value >= 55 || y.value <= 25) {
box.value = 'passed';
}
}
&#13;
<input onchange="trigger()" required type="number" id="xcoord" name="xcoord">
<input onchange="trigger()" required type="number" id="ycoord" name="ycoord">
<input required type="text" id="touch" name="touch">
&#13;
使用JQuery将值放到输入框
$('#touch').val('passed'); //Although you already have the box variable
CodeSnippet
function trigger(){
var x = document.getElementById('xcoord');
var y = document.getElementById('ycoord');
var box = document.getElementById('touch');
if (x.value >= 325 || x.value <= 300 && y.value >= 55 || y.value <= 25) {
$('#touch').val('passed');
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input onchange="trigger()" required type="number" id="xcoord" name="xcoord">
<input onchange="trigger()" required type="number" id="ycoord" name="ycoord">
<input required type="text" id="touch" name="touch">
&#13;
<强> TL;博士; 强>
由于您使用普通JavaScript
启动了代码,因此您混淆了如何使用jQuery
语法更改值。
JavaScript box.value = 'passed';
jQuery $('#touch').val('passed');
答案 1 :(得分:2)
您正在使用jQuery库。使用优于 jQuery 方法。
$("#xcoord , #ycoord").on("keyup", function(){
var x = $('#xcoord').val();
var y = $('#ycoord').val();
$('#touch').val((x >= 325 || x <= 300) && (y >= 55 || y <= 25)?'passed':'');
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input required type="number" id="xcoord" name="xcoord" value="0">
<input required type="number" id="ycoord" name="ycoord" value="0">
<input required type="text" id="touch" name="touch">
答案 2 :(得分:1)
应该是
$('#touch').val('passed');