[英]Select-box control with jquery
I am using a selectbox: 我正在使用一个选择框:
<select name="priority" id="priority">
<option value="4">Low</option>
<option value="3" selected="selected">Normal</option>
<option value="2">High</option>
<option value="1">Emergency</option>
</select>
When user select "Emergency", then a confirmation box opens and if user confirm, then "Emergency" is selected. 当用户选择“紧急”时,将打开一个确认框,如果用户确认,则选择“紧急”。 Otherwise "Normal" should be selected. 否则应选择“正常”。 For This, I am using jquery: 为此,我正在使用jQuery:
$(document).ready(function() {
$('#priority').change(function(){
if($(this).val()=='1'){
if(confirm("Are you sure this is emergency?")){
return true;
}
else{
//here some code is needed to select "Normal".
}
}
});
});
Now, How can I do that? 现在,我该怎么做?
Since you're just returning in the confirmation case, you can shorten your logic down to this: 由于您只是返回确认案例,因此您可以将逻辑缩短为:
$(function() {
$('#priority').change(function(){
if($(this).val()=='1' && !confirm("Are you sure this is emergency?")){
$(this).val('3');
}
});
});
else{
$(this).find("option[value=3]").attr("selected","selected");
//and trigger change after that if you need it
$(this).change();
}
The following code snippet should do what you are after - it selects the priority select element by ID and then sets the selected value to 3. 以下代码段将完成您的工作-通过ID选择优先级选择元素,然后将所选值设置为3。
else
{
$('#priority').val(3);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.