您好我还有问题。我构建了js,您可以按以下顺序添加数字 - > 50-20并且在选择中你得到的数字从20,21,22,23,24,25到50开始。我想改变代码的顺序当你把50-20的结果放在select中时应该从50开始并从50,49,48开始计算....直到20。
以下是JSFIDDLE上的代码。问题可能非常愚蠢,但现在我很困惑:X。
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<link rel="stylesheet" type="text/css" href="css/alertify.core.css"/>
<link rel="stylesheet" type="text/css" href="css/alertify.default.css"/>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text /css'>
</head>
<body>
<div class="wrapper">
<h1>Enter values</h1>
<p class="example">number - number</p>
<input type="text" name="field" id="keys" class="button"/>
<a href="javascript:void(0);" id="calculate" class="press">Push!</a>
<div id="select_result">
<!-- Generate Dynamic Select! -->
</div>
</div><!-- End wrapper div -->
<script src="js/jquery-1.11.0.js"></script>
<script src="js/task.js"></script>
<script src="js/alertify.min.js"></script>
</body>
</html>
JS
$(document).ready(function () {
function calculateResult(){
var value_1 = parseInt( $("#keys").val().split("-")[0] );
var value_2 = parseInt( $("#keys").val().split("-")[1] );
if( $("#keys").val().indexOf("-") == "-1" ){
// alert dialog
alertify.alert("Въведи две цифри с тире!");
return;
} else if ( typeof value_1 == 'undefined' || typeof value_2 == 'undefined' || value_1 == "" || value_2 == "" || isNaN(value_1) || isNaN(value_2)) {
// error notification
// shorthand for alertify.log("Notification", "error");
alertify.error("Въведи две цифри!");
return;
}
var select_string = "";
select_string = "<select>";
if ( value_1 > value_2 ){
for( i = value_2; i <= value_1; i++ ){
select_string += "<option>" + i + "</option>";
}
} else if ( value_1 < value_2 ){
for( i = value_1; i >= value_2; i++ ){
select_string += "<option>" + i + "</option>";
}
} else if ( value_1 == value_2 ){
select_string += "<option>" + value_1 + "</option>";
}
select_string += "</select>";
$("#select_result").html(select_string);
}
$("#calculate").on("click", function(){
calculateResult();
});
$(document).on("keypress", function (e) {
if(e.which == 13) {
calculateResult(e);
}
});
});
谢谢
答案 0 :(得分:1)
试试这个:
for( i = value_1; i >= value_2; i-- ){
select_string += "<option>" + i + "</option>";
console.log(select_string);
}
<强> DEMO 强>