简体   繁体   English

如何使js / javascript在Django Python forloop中检索特定的html选项值

[英]How to make js/javascript retrieve the specific html option value inside a django python forloop

So I want to get the option currently selected on my looped option value, but it only keeps getting the last index of the loop whenever I press submit. 所以我想在循环选项值上获取当前选择的选项,但是每当我按下提交时,它只会一直获取循环的最后一个索引。

views.py views.py

def editGrp(request):       
JSONer = {}
parsedData = urlparse.urlparse(request.get_full_path())
pkid = (urlparse.parse_qs(parsedData.query)['pkid'][0])
print(pkid)
groupid = (urlparse.parse_qs(parsedData.query)['groupid'][0])
if User.objects.filter(id=pkid).count() > 0 and Group.objects.filter(id=groupid).count() > 0:
    print(pkid)
    print(groupid)
    userID = User.objects.filter(id=pkid)[0]
    userID.profile.group_id = groupid
    print(userID.username)
    userID.save(force_update=True)
return HttpResponseRedirect(json.dumps(JSONer)) 

specific code inside my "admin.html" 我的“ admin.html”中的特定代码

The loop is for listing all the users who aren't inside the specific group 该循环用于列出不在特定组内的所有用户

<label class="control-label col-md-3 col-sm-3 col-xs-12">Add Members:</label>
<div class="col-md-8 col-sm-6 col-xs-12">
    <select name="employeeName" id="employeeLocation" class="select2_single form-control" tabindex="-1">
    {% for user in users %}
      {% ifnotequal user.profile.group.id groups.id %}
        <option name="userGroup" id="userGroup-{{ user.id }}" value="{{user.id}}">{{user.username}}</option>
      {% endifnotequal %}
    {% endfor %}
    </select>
</div>

submit button in admin.html admin.html中的“提交”按钮

<button type="submit" data-dismiss="modal" onClick="editGrpAjax('userGroup-{{user.id}}', {{groups.id}})" class="btn btn-primary">Save changes</button>

javascript code JavaScript代码

<script>
    function editGrpAjax(id,groupid){
      var newid = document.getElementById(id).value;
      var newgroupid = document.getElementById('groupid-' + groupid).value;
        $.ajax({url: 'http://127.0.0.1:8000/editGrp/?pkid='+ newid + "&groupid=" + newgroupid, 
            success: function(result){

              // insert success message here
            }
        });
    }
</script>

It keeps getting the last value of the loop, and not the one that is currently selected or highlighted in the option value upon submit. 它一直获取循环的最后一个值,而不是提交时当前在选项值中选择或突出显示的那个值。

Don't bother passing id and groupid into your function. 不要打扰将idgroupid传递到函数中。 Like you said - it always only receives the last values looped, because that's hard-coded based on the userGroup-{{user.id}}', {{groups.id}} string. 就像您说的那样-它始终只接收循环的最后一个值,因为它是基于userGroup-{{user.id}}', {{groups.id}}字符串进行硬编码的。 That string is set by the server at page load, and never changes during the life of the page. 该字符串由服务器在页面加载时设置,并且在页面有效期内不会更改。

If you want to find the option which is currently selected, simply do: 如果要查找当前选择的选项,只需执行以下操作:

var newid = document.getElementById("employeeLocation").value;

The value of the <select> will be the value of the currently selected <option> . value的的<select>当前选择的将是值<option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM