gpt4 book ai didi

jquery ui组合框从菜单项创建数组

转载 作者:行者123 更新时间:2023-12-01 07:47:40 25 4
gpt4 key购买 nike

我正在取消这个版本的组合框... combobox

...我正在尝试获取菜单项,然后创建一个 json 数组,这是我到目前为止想到的代码...

"keyup .ui-combobox-input":function(event){
if(event.keyCode==13){
event.preventDefault();
newItem=$(this.uiInput).val()
this.element.append('<option value="'+newItem+'">'+newItem+'</option>');
var text=JSON.stringify(this.element.text());
};
}

我从 var text 得到的输出是这样的......

"\n\t\t\t\t\t\t\t\t\t\t\tone moretwothreefour" 

但我想要这个...

[{"value":"1", "label":"one more"}, {"value":"2", "label":"two"}, {"value":"3", "label":"three"}, {"value":"4", "label":"four"}]

我不明白转义的 n 和 t 是什么,当我尝试使用此 text=$.parseJSON(text); 将文本转换为数组时,文本未转换为数组.

最佳答案

我完全不确定您的代码,因为有多个对此属性的引用。但根据我的理解,你可以做的如下。

1) 选择列表/组合框

       <div class="ui-widget" id="test">
<label>Your preferred programming language: </label>
<select id="combobox">
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
</select>
</div>

2)组合框的Keyup事件

    $(".ui-combobox-input").keyup(function(event){
if(event.keyCode==13){
event.preventDefault();

//This gives the value being entered in text field on dropdown
var textVal = $(".ui-combobox-input").val();

//var dd = $('#combobox').val();
//this.element.append('<option value="'+newItem+'">'+newItem+'</option>'); **THIS did not work for me**

var combobox = []
$('#combobox').append($('<option>', {value:textVal, text:textVal, selected:true})); //USE SELECTED:TRUE if you want dynamically added value to be selected Please TEST it if its getting selected or not

$('#combobox > option').each(function() {
combobox.push(
{
value: $(this).val(),
label: $(this).text()
})
});

jsonString = JSON.stringify(combobox);
alert(jsonString);
};
});
});

您可以对上述代码进行的其他改进是使用 DIV 的 ID 来保存下拉列表并更新该元素的相关 keyup。 (如果同一页面上有多个此类元素)。为元素使用唯一且有意义的名称。我刚刚在上面的代码中使用了随机名称。我希望它有帮助。

关于jquery ui组合框从菜单项创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com