Javascript 通过单击数据表行,从下拉列表中选择一个选项
我有一个数据表,当用户单击表行时,它将获得Javascript 通过单击数据表行,从下拉列表中选择一个选项,javascript,jquery,html,Javascript,Jquery,Html,我有一个数据表,当用户单击表行时,它将获得{{content.id},并弹出另一个子表 <table id="project-content-datatable" class="display table table-hover table-responsive" style="width: 100%"> <thead> <tr> <th class="small text-muted text-uppercase
{{content.id}
,并弹出另一个子表
<table id="project-content-datatable" class="display table table-hover table-responsive" style="width: 100%">
<thead>
<tr>
<th class="small text-muted text-uppercase"><strong>ID</strong></th>
<th class="small text-muted text-uppercase"><strong>Name</strong></th>
<th class="small text-muted text-uppercase"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
{% for content in content_list %}
{% if content.search_type.name == searchtype.name %}
<tr class="text-primary">
<td class="text-left" style="cursor: pointer"
onclick='load_workorder({{ content.id }});'>
{{ content.id }}
</td>
<td class="text-left" style="cursor: pointer" onclick='load_workorder({{ content.id }});'>
{{ content.name }} </td>
<td class="text-left"> {{ content.description }} </td>
</tr>
{% endif %}
{% endfor content_list %}
</tbody>
<script>
function load_workorder(content_id) {
workorder_path = "/dashboard/workorder_list/" + content_id + "/?format=json";
workorder_table.api().ajax.url(workorder_path).load();
</script>
值等于{{content.id}
。我想对用户隐藏此字段,因此如何根据用户的{content.id}
单击将
更改为选中
非常感谢您提供的任何帮助,因此如果content.id值与select下拉列表中的选项匹配,jQuery将非常简单 下面是一个小示例,但您只需调用
selectOption()
函数并传递content.id值:
// my simplified example to grab a value from the table
$('td').click(function(){
var value = $(this).text();
selectOption(value);
});
// pass the value to this function
function selectOption(v){
$('#id_parent_project_content option[value="'+v+'"]').prop('selected', true);
}
谢谢@partypete25这很有效!我发现了一个类似于您的解决方案,但使用了标签,但您的解决方案要好得多,谢谢!
// my simplified example to grab a value from the table
$('td').click(function(){
var value = $(this).text();
selectOption(value);
});
// pass the value to this function
function selectOption(v){
$('#id_parent_project_content option[value="'+v+'"]').prop('selected', true);
}