问题
So, here's the HTML for a custom dropdown that doesn't use a select tag. The placeholder/default value for the dropdown is "Acura", which is the first element in the list (ul). I'm trying to see if I can change the placeholder/default value for the dropdown (to say, BMW for example) without changing the order of the list of the dropdown (the makes are in alphabetical order).
<span id="builder-make-selection" class="pick">
<span class="label">Make:</span>
<div class="custom-dd-wrapper builder-makes" tabIndex="1">
<div class="custom_dd_select">
<span>
<a>Acura</a></span>
</div>
<div class="custom_dd_options">
<ul class="custom_dropdown">
<li data-make="Acura">Acura</li>
<li data-make="Alfa Romeo">Alfa Romeo</li>
<li data-make="Audi">Audi</li>
<li data-make="BMW">BMW</li>
</ul>
</div>
Here's the javascript that's creating the dropdown:
(function(h, a) {
function k() {
var b = a(this).parents("div.custom_dd_wrapper"),
f = b.attr("id"),
e = a(b).find(".custom_dd_select a"),
l = a(this).data(),
d = e.text().trim(),
g = a(this).text().trim();
"makes-options" == f && (a(b).addClass("active"), e.parent("span").attr("style", a(this).attr("style")));
e.text(g);
for (var m in l) {
if(m=='make'){
e.parent("span").data(m,l[m]);
}
}
d != g && a(this).closest(".custom_dd_wrapper").trigger("change")
}
function g() {
var b = a(this).closest(".custom_dd_wrapper");
a(b).find("ul").hide();
a(b).find(".custom_dd_select").removeClass("click");
a(b).find(".custom_dd_options").removeClass("show")
}
h.CustomDropdownView = function() {
var b = this;
this.vc = !1;
a(".cs").on("click", ".trans-disabled", function(a) {
a.stopPropagation()
});
a(".cs").on("click", ".custom_dd_wrapper, .popup .custom_dd_wrapper", function() {
b.vc = !0;
var f = a(this).closest(".custom_dd_wrapper");
a(f).find("ul").toggle();
a(f).find(".custom_dd_select").toggleClass("click");
a(f).find(".custom_dd_options").toggleClass("show")
});
a(".cs").on("click", ".custom_dd_options .custom_dropdown > li:not(.trans-disabled)",
k);
if (0 > window.navigator.userAgent.indexOf("MSIE ") && null == navigator.userAgent.match(/Trident.*rv\:11\./)) a(".cs").on("blur", ".custom_dd_wrapper, .popup .custom_dd_wrapper", function() {
a(this).find(".custom_dd_options ul").is(":visible") && g.call(this)
});
else a(".cs").on("click", function() {
if (b.vc) b.vc = !1;
else {
var f = a(".custom_dropdown").filter(":visible");
0 < f.length && f.each(g);
}
});
a(".cs").on("click", ".disable", g)
}
})
回答1:
You have to change
<div class="custom_dd_select">
<span>
<a>Acura</a></span>
</div>
to
<div class="custom_dd_select">
<span>
<a>BMW</a></span>
</div>
That will work
来源:https://stackoverflow.com/questions/43490969/js-jquery-changing-default-value-placeholder-of-custom-dropdown