无法通过给输入字段赋值来添加选择字段?

时间:2018-11-21 06:07:52

标签: javascript php jquery bootstrap-4 codeigniter-3

大家好,希望您一切都好。 问题是...

  1. 当您尝试在其中添加一些值时,我只有一个输入字段,然后将出现一个选择框。 但问题是它仅显示一个我未达到值要求的值。
  2. 第二个问题是我尝试使用选择字段而不是我尝试过的输入字段

    File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in aggregate 360. arg.default_alias During handling of the above exception ('CombinedExpression' object has no attribute 'default_alias'), another exception occurred: File "C:\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner 34. response = get_response(request) File "C:\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response 126. response = self.process_exception_by_middleware(e, request) File "C:\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response 124. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\customers\views.py" in Summary 222. past_3_month_avg = Book..objects.filter(trandate_b__range=(past_3_month_first_day,current_month_first_day)).aggregate(Sum('total')/3) File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in aggregate 362. raise TypeError("Complex aggregates require an alias") Exception Type: TypeError at /summary/ Exception Value: Complex aggregates require an alias

这是一个选项,但无法使用选择字段代替输入字段。 var e = document.getElementById("this_is_my_select_field_values");

这是我的代码:

var strUser = e.options[e.selectedIndex].value;
function addFields() {
	var number = document.getElementById("member").value;
	var myDiv = document.getElementById("myDiv");

	//Create array of options to be added
	var array = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];

	while (myDiv.hasChildNodes()) {
		myDiv.removeChild(myDiv.lastChild);
	}
	for (i = 0; i < number; i++) {
		myDiv.appendChild(document.createTextNode("Member " + (i + 1)));

		var selectList = document.createElement("select");
		selectList.setAttribute("id", "mySelect");
		selectList.setAttribute("class", "form-control");
		myDiv.appendChild(selectList);
		myDiv.appendChild(document.createElement("br"));

		//Create and append the options
		for (var i = 0; i < array.length; i++) {
			var option = document.createElement("option");
			option.setAttribute("value", array[i]);
			option.text = array[i];
			selectList.appendChild(option);
		}
	}
}

第一个问题::没有出现选择字段的数量。

第二个问题::在尝试使用选择字段而不是输入字段(我们试图在其中输入一些值来显示选择字段)时,它不起作用。 我尝试了以下方法,但是不起作用。

<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <!-- jQuery library -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <!-- Latest compiled JavaScript -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   </head>
   <body>
      <!-- <div id="myDiv" class="col-xs-1">Append here</div> -->
      <input type="text" id="member" name="member" value="" onkeyup="addFields()">
      <div id="myDiv" class="col-xs-1">Append here</div>
   </body>
</html>

请大家帮忙。预先感谢

1 个答案:

答案 0 :(得分:1)

更改如下代码:

更改为

for (var j = 0; j < array.length; j++) {
            var option = document.createElement("option");
            option.setAttribute("value", array[j]);
            option.text = array[j];
            selectList.appendChild(option);
        }

function addFields() {
	var number = document.getElementById("member").value;
	var myDiv = document.getElementById("myDiv");

	//Create array of options to be added
	var array = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];

	while (myDiv.hasChildNodes()) {
		myDiv.removeChild(myDiv.lastChild);
	}
	for (i = 0; i < number; i++) {
		myDiv.appendChild(document.createTextNode("Member " + (i + 1)));

		var selectList = document.createElement("select");
		selectList.setAttribute("id", "mySelect");
		selectList.setAttribute("class", "form-control");
		myDiv.appendChild(selectList);
		myDiv.appendChild(document.createElement("br"));

		//Create and append the options
		for (var j = 0; j < array.length; j++) {
			var option = document.createElement("option");
			option.setAttribute("value", array[j]);
			option.text = array[j];
			selectList.appendChild(option);
		}
	}
}
<!DOCTYPE html>
<html>
   <head>
      <title></title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <!-- jQuery library -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <!-- Latest compiled JavaScript -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   </head>
   <body>
      <!-- <div id="myDiv" class="col-xs-1">Append here</div> -->
      <input type="text" id="member" name="member" value="" onkeyup="addFields()">
      <div id="myDiv" class="col-xs-1">Append here</div>
   </body>
</html>

关于问题2:

使用纯Java脚本:

var e = document.getElementById("elementId");
var value = e.options[e.selectedIndex].value;

使用jQuery

var selected = ("#this_is_my_select_field_values").val();

input更改为select

<!DOCTYPE html>
<html>
   <head>
   <script>
   function addFields() {
	var number = document.getElementById("member").value;
	var myDiv = document.getElementById("myDiv");

	//Create array of options to be added
	var array = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];

	while (myDiv.hasChildNodes()) {
		myDiv.removeChild(myDiv.lastChild);
	}
	for (i = 0; i < number; i++) {
		myDiv.appendChild(document.createTextNode("Member " + (i + 1)));

		var selectList = document.createElement("select");
		selectList.setAttribute("id", "mySelect");
		selectList.setAttribute("class", "form-control");
		myDiv.appendChild(selectList);
		myDiv.appendChild(document.createElement("br"));

		//Create and append the options
		for (var j = 0; j < array.length; j++) {
			var option = document.createElement("option");
			option.setAttribute("value", array[j]);
			option.text = array[j];
			selectList.appendChild(option);
		}
	}
}
   </script>
      <title></title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <!-- jQuery library -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <!-- Latest compiled JavaScript -->
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   </head>
   <body>
      <!-- <div id="myDiv" class="col-xs-1">Append here</div> -->
    <select id="member" name="member" value="" onchange="addFields()"> <option value="">--select--</option><option value="1">1</option> <option value="2">2</option> </select>
      <div id="myDiv" class="col-xs-1">Append here</div>
   </body>
</html>