내 질문의 제목이 내 질문을 정확하게 설명하는지 100 % 확신하지 못합니다. 미리 사과드립니다. 내 용어가 여기에서 벗어난 경우 저를 수정하십시오.
기본적으로 선택 상자가 있습니다. 사용자가 선택하면 개체에 저장된 값을 사용하여 옵션을 설정하는 새 선택 상자를 만들고 싶습니다.
내 시도를 보여주는 jsFiddle
내 코드 :
HTML :
<SELECT id="mainIssueType" style="color:#888"size=0>
<OPTION style="color:#888" selected> Issue Type </option>
<OPTION style="color:#888" value="Hotel"> Hotel </option>
<OPTION style="color:#888" value="Flights"> Flights </option>
</SELECT>
스크립트 :
var tree = {
"Hotel": [{val: "Htl1", text: 'Cancel'},
{val: "Htl2", text: 'Modify'},
{val: "Htl3", text: 'Research'},
{val: "Htl4", text: 'Complaint'}],
"Flights": [{val: "Flt1", text: 'Void'},
{val: "Flt1", text: 'Cancel'},
{val: "Flt1", text: 'Change Flight'},
{val: "Flt1", text: 'Schedule Change'},
{val: "Flt1", text: 'Name Change'}, ]
};
$(function() {
$('#mainIssueType').change(function() {
//get current selected option
var selectVal = $('#mainIssueType :selected').val();
//create a new select box and add to body
var sel = $('<select>').appendTo('body');
//give the new element an id matching
//the selected value from the previous element
sel.attr('id', selectVal);
//set the select box's options using the values
//from the "selectVal" object within the "tree" object
$(tree.selectVal).each(function() {
//tree.selectVal seems to be the problem
sel.append($("<option>").attr('value', this.val).text(this.text));
});
});
});
tree.selectVal
에서는 $(tree.selectVal).each
여기에 문제가 될 것으로 보인다. 여기에 표시된 것처럼 tree.Hotel
작동하도록 할 수 있으므로 직접 말하는 것과 같지 tree.Hotel
않은 것 같습니다.
tree
이름이 내 selectVal
변수 와 일치 하는 객체에 액세스하려면 어떻게 해야합니까?
$(tree[selectVal])
대신 사용$(tree.selectVal)
$(function(){
$('#mainIssueType').change(function() {
//get current selected option
var selectVal = $('#mainIssueType :selected').val();
//create a new select box and add to body
var sel = $('<select>').appendTo('body');
//give the new element an id matching
//the selected value from the previous element
sel.attr('id',selectVal);
//set the select box's options using the values
//from the "selectVal" object within the "tree" object
$(tree[selectVal]).each(function() {
//_____^_____________________
//tree.selectVal seems to be the problem
sel.append($("<option>").attr('value',this.val).text(this.text));
});
});
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다