我有一个带有两个选项的seclet2下拉列表
<select id="ddlsearch">
<option value="0">Translated Search</option>
<option value="1" selected="">Exact Search</option>
</select>
它似乎正确地更改了第一个if
语句的占位符,但我似乎无法使我的else语句正常工作。我到底错在了什么?
function selectPlaceholder() {
var reportLevel = "0",
$select = $("#ddlsearch")
if (reportLevel == "0") {
$select.on("change", function(e) {
$('#txtkey').attr('placeholder', 'asd')
});
} else {
$select.on("change", function(e) {
$('#txtkey').attr('placeholder', '123')
});
}
};
selectPlaceholder()
答案 0 :(得分:2)
你需要做这样的事情......
$("#ddlsearch").on("change", function(e) {
if ("0" == this.value) {
$('#txtkey').attr('placeholder', 'asd')
} else {
$('#txtkey').attr('placeholder', 'fgh')
}
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4> Your select </h4>
<select id="ddlsearch">
<option value="0">Translated Search</option>
<option value="1" selected="">Exact Search</option>
</select>
<h4> Your text input somewhere </h4>
<input id='txtkey'>
if
语句应驻留在change
事件处理程序中,并且应根据select的值更改占位符。