我正在尝试下面给出的代码:
var tmp1 = "3";
var status_detail_hanca = "<select class='status_detail_hanca' name='status_detail_hanca[]' ><option value='0'>Proses</option><option value='1'>Return</option><option value='2' >Selesai</option></select>";
$('select.status_detail_hanca').val(tmp1);
$(".tbody_detail_hanca_checking").append(status_detail_hanca);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='tbody_detail_hanca_checking'>
</div>
但是此代码无效,tmp1
来自json,而select-option
的值来自循环$.each
。我也尝试使用此代码:
var status_detail_hanca = "<select class='status_detail_hanca' name='status_detail_hanca[]' ><option value='0'"+ if(tmp1 === '0'){$(this).append("selected")} +">Proses</option><option value='1'"+ if(tmp1 === '0'){$(this).append("selected")} +">Return</option><option value='2'"+ if(tmp1 === '0'){$(this).append("selected")} +">Selesai</option></select>";
但是,它也没有用。 如何解决这个问题呢 ?
谢谢你们!
答案 0 :(得分:2)
你可以尝试这样的事情。使用循环并创建字符串比硬编码更好
def encrypt(key,content):
EncryptedData = win32com.client.Dispatch('CAPICOM.EncryptedData')
EncryptedData.Algorithm.KeyLength = 0
EncryptedData.Algorithm.Name = 4
EncryptedData.SetSecret(key)
EncryptedData.Content = content
return EncryptedData.Encrypt()
var tmp1 = "3";
var optionList = ["Proses", "Return","Selesai", "TEST", "FOO"]
var status_detail_hanca = "<select class='status_detail_hanca' name='status_detail_hanca[]' >";
status_detail_hanca = optionList.reduce(function(p, c, i){
var _t = "<option value='"+i+"' ";
if(tmp1 == i){
_t += "selected='selected'";
}
_t += ">" + c + "</option>";
p += _t;
return p;
}, status_detail_hanca);
status_detail_hanca += "</select>";
$(".tbody_detail_hanca_checking").append(status_detail_hanca);
$(document).on("change", ".status_detail_hanca", function(){
console.log($(this).val())
})
答案 1 :(得分:1)
您可以使用.prepend()
,jQuery()
将<option>
元素添加到select.status_detail_hanca
,其中value
和text
设置为tmp1
},selected
属性设置为true
。
var tmp1 = "3";
var status_detail_hanca = `
<select class='status_detail_hanca' name='status_detail_hanca[]'>
<option value='0'>Proses</option>
<option value='1'>Return</option>
<option value='2' >Selesai</option>
</select>`;
$(".tbody_detail_hanca_checking")
.append(status_detail_hanca);
var select = $("select.status_detail_hanca")
.prepend($("<option>", {value:tmp1, selected:true, text: tmp1}));
console.log(select.val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class='tbody_detail_hanca_checking'>
</div>
答案 2 :(得分:0)
//基于选择选项文本的选择
$(".status_detail_hanca option:contains("+ $.trim("Text you want to select") +")").attr('selected', 'selected');
//基于选择选项值的选择
$('.status_detail_hanca').val(name);
在类名之前添加点,逐个选择元素,即$('。ClassName')。opertion();