在调用函数时更改 select 框的选项值
Change option value of select box on calling function
我正在使用 select 框,其中有两个选项。当我单击一个名为“重新定位封面”的选项时,它会调用 JavaScript 函数。函数调用后,它在 select 框中显示“重新定位封面”(select 框的默认行为),但我想在 select 框一旦函数被调用。
当用户再次点击“重新定位封面”选项时,该函数将再次调用并且 select 框将 return 它的默认值等等。
以下是我为实现此目的而编写的代码:
Html
<select id="cars">
<option value="change" selected="selected">Change</option>
<option value="rep">Reposition cover</option>
<div>Reposition cover</div>
</select>
Javascript
<script type="text/javascript">
$(document).ready(function(){
e1 = document.getElementById('cars');
if(e1)
{
e1.addEventListener('change', function() {
if(this.value == 'rep'){
repositionCover();
/*Execute your script */
}
else
{
}
});
}
});
</script>
只需重置 select
的值
$(document).ready(function(){
$('#cars').on('change', function() {
if(this.value == 'rep'){
this.value = 'change';
//repositionCover();
} else {
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="cars">
<option value="change" selected="selected">Change</option>
<option value="rep">Reposition cover</option>
<div>Reposition cover</div>
</select>
我正在使用 select 框,其中有两个选项。当我单击一个名为“重新定位封面”的选项时,它会调用 JavaScript 函数。函数调用后,它在 select 框中显示“重新定位封面”(select 框的默认行为),但我想在 select 框一旦函数被调用。 当用户再次点击“重新定位封面”选项时,该函数将再次调用并且 select 框将 return 它的默认值等等。 以下是我为实现此目的而编写的代码:
Html
<select id="cars">
<option value="change" selected="selected">Change</option>
<option value="rep">Reposition cover</option>
<div>Reposition cover</div>
</select>
Javascript
<script type="text/javascript">
$(document).ready(function(){
e1 = document.getElementById('cars');
if(e1)
{
e1.addEventListener('change', function() {
if(this.value == 'rep'){
repositionCover();
/*Execute your script */
}
else
{
}
});
}
});
</script>
只需重置 select
的值$(document).ready(function(){
$('#cars').on('change', function() {
if(this.value == 'rep'){
this.value = 'change';
//repositionCover();
} else {
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="cars">
<option value="change" selected="selected">Change</option>
<option value="rep">Reposition cover</option>
<div>Reposition cover</div>
</select>