每次用户按下 btnAdd
按钮时,我都会出现一个 scombobox 和一个文本字段。
$("#btnAdd").bind("click", function () {
index++;
var div = $("<div />");
div.html(GetDynamicTextBox(''));
$("#AddContainer").append(div);
$("#" + index).scombobox({ fullMatch: true });
var completedField = true; //variable to see if all fields (values ones) are completed - if no not allow to save changes
//check if each text field for each setting has value
$("input[name=DynamicTextBox]").each(function () {
if ($(this).val() == "")
completedField = false;
});
if (completedField == false)
document.getElementById("AddBtn").disabled = true;
else
document.getElementById("AddBtn").disabled = false;
});
$("body").on("click", ".remove", function () {
$(this).closest("div").remove();
var ddlId = $('[id*=hfDDLId]').val();
$('[id$=ddl' + parseInt(ddlId) + ']').remove();
$('[id*=hfDDLId]').val(parseInt($('[id*=hfDDLId]').val()) - 1);
var previousDropDownId = $('[id*=hfDropDownIds]').val();
//$('[id*=hfdropdownids]').val(resultids);
if (document.getElementById("AddContainer").innerHTML == "")
document.getElementById("AddBtn").disabled = true;
else {
var completedField = true; //variable to see if all fields (values ones) are completed - if no not allow to save changes
//check if each text field for each setting has value
$("input[name=DynamicTextBox]").each(function () {
if ($(this).val() == "")
completedField = false;
});
if (completedField == false)
document.getElementById("AddBtn").disabled = true;
else
document.getElementById("AddBtn").disabled = false;
}
});
function GetDynamicTextBox(value) {
var combo = $('<select list="' + index + '" name="' + index + '"><datalist style="font-size:150%; width:50%"></select>').attr("id", index).attr("name", index).attr("runat", "server").attr("class", "class combos").attr("required", "required");
$.each(settingsListAry, function (i, el) {
combo.append("<option value=" + el + ">" + el + "</option>");
});
return '<input type="button" value="-" class="remove btn btn-danger" style="font-size:75%" /> '
+ combo.prop('outerHTML') + ' '
+ '<input name = "DynamicTextBox" class="dtb" type="text" value="' + value + '" required style="line-height: 0; width:30%" onkeyup="allHaveText()"/>'
+ '<div style="clear:both"></div>'
}
我的问题是,当用户键入新文本(不是选项中的文本)时,当我尝试获取键入的字符串 $("#"+ i).scombobox('val')
我只收到空字符串。
可以从 scombobox 中获取键入的文本吗?
最佳答案
这是我第一次听说 scombobox,可能有更好的解决方案,但请检查此链接 http://jsfiddle.net/alpenzoo/7vpf1axs/
$("#combo-02").find("input").val()
似乎可以正确地从 scombobox 返回数据。
关于JQuery Simple Combobox(组合框)获取文本字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46713951/