jQuery Impromptu(jQuery Dialog插件)(2)--prompt user input in a fashionable way
分析例子,以便更好的理解:
例子1:http://trentrichardson.com/Impromptu/demos/demo1.html
Code
例子2:http://trentrichardson.com/Impromptu/demos/demo2.html
function editUser(id){
var user = $('#userid'+id)
var fname = user.find('.fname').text();
var lname = user.find('.lname').text();
var txt = 'What would you like to change this to?'+
'<div class="field"><label for="editfname">First Name</label><input type="text" id="editfname" name="editfname" value="'+ fname +'" /></div>'+
'<div class="field"><label for="editlname">Last Name</label><input type="text" id="editlname" name="editlname" value="'+ lname +'" /></div>';
$.prompt(txt,{
buttons:{Change:true, Cancel:false},
submit: function(v,m,f){
//this is simple pre submit validation, the submit function
//return true to proceed to the callback, or false to take
//no further action, the prompt will stay open.
var flag = true;
if (v) {
if ($.trim(f.editfname) == '') {
m.find('#editfname').addClass('error');
flag = false;
}
else m.find('#editfname').removeClass('error');
if ($.trim(f.editlname) == '') {
m.find('#editlname').addClass('error');
flag = false;
}
else m.find('#editlname').removeClass('error');
}
return flag;
},
callback: function(v,m,f){
if(v){
//Here is where you would do an ajax post to edit the user
//also you might want to print out true/false from your .php
//file and verify it has been removed before removing from the
//html. if false dont remove, $promt() the error.
//$.post('edituser.php',{userfname:f.editfname,userlname:f.editlname}, callback:function(data){
// if(data == 'true'){
user.find('.fname').text(f.editfname);
user.find('.lname').text(f.editlname);
// }else{ $.prompt('An Error Occured while editing this user'); }
//});
}
else{}
}
});
}
例子3:http://trentrichardson.com/Impromptu/demos/demostates.html states转换var user = $('#userid'+id)
var fname = user.find('.fname').text();
var lname = user.find('.lname').text();
var txt = 'What would you like to change this to?'+
'<div class="field"><label for="editfname">First Name</label><input type="text" id="editfname" name="editfname" value="'+ fname +'" /></div>'+
'<div class="field"><label for="editlname">Last Name</label><input type="text" id="editlname" name="editlname" value="'+ lname +'" /></div>';
$.prompt(txt,{
buttons:{Change:true, Cancel:false},
submit: function(v,m,f){
//this is simple pre submit validation, the submit function
//return true to proceed to the callback, or false to take
//no further action, the prompt will stay open.
var flag = true;
if (v) {
if ($.trim(f.editfname) == '') {
m.find('#editfname').addClass('error');
flag = false;
}
else m.find('#editfname').removeClass('error');
if ($.trim(f.editlname) == '') {
m.find('#editlname').addClass('error');
flag = false;
}
else m.find('#editlname').removeClass('error');
}
return flag;
},
callback: function(v,m,f){
if(v){
//Here is where you would do an ajax post to edit the user
//also you might want to print out true/false from your .php
//file and verify it has been removed before removing from the
//html. if false dont remove, $promt() the error.
//$.post('edituser.php',{userfname:f.editfname,userlname:f.editlname}, callback:function(data){
// if(data == 'true'){
user.find('.fname').text(f.editfname);
user.find('.lname').text(f.editlname);
// }else{ $.prompt('An Error Occured while editing this user'); }
//});
}
else{}
}
});
}
function openprompt(){
var temp = {
state0: {
html:'test 1.<br />test 1..<br />test 1',
buttons: { Cancel: false, Next: true },
focus: 1,
submit:function(v,m,f){
if(!v)
return true;
else $.ImpromptuGoToState('state1');//go forward
return false;
}
},
state1: {
html:'test 2',
buttons: { Back: -1, Exit: 0, Next: 1 },
focus: 2,
submit:function(v,m,f){
if(v==0)
jQuery.ImpromptuClose()
else if(v==1)
$.ImpromptuGoToState('state2');//go forward
else if(v=-1)
$.ImpromptuGoToState('state0');//go back
return false;
}
},
state2: {
html:'This is the next one<br />test 3',
buttons: { Back: true, Done: false },
submit:function(v,m,f){
if(!v)
return true;
else $.ImpromptuGoToState('state1');//go back
return false;
}
}
}
$.prompt(temp);
}
例子4:http://trentrichardson.com/Impromptu/demos/survey.htmlvar temp = {
state0: {
html:'test 1.<br />test 1..<br />test 1',
buttons: { Cancel: false, Next: true },
focus: 1,
submit:function(v,m,f){
if(!v)
return true;
else $.ImpromptuGoToState('state1');//go forward
return false;
}
},
state1: {
html:'test 2',
buttons: { Back: -1, Exit: 0, Next: 1 },
focus: 2,
submit:function(v,m,f){
if(v==0)
jQuery.ImpromptuClose()
else if(v==1)
$.ImpromptuGoToState('state2');//go forward
else if(v=-1)
$.ImpromptuGoToState('state0');//go back
return false;
}
},
state2: {
html:'This is the next one<br />test 3',
buttons: { Back: true, Done: false },
submit:function(v,m,f){
if(!v)
return true;
else $.ImpromptuGoToState('state1');//go back
return false;
}
}
}
$.prompt(temp);
}
Code
例子4:Test Impromptu With Ajax
http://trentrichardson.com/Impromptu/demos/demoajax.html
function openprompt(){
$.getJSON('demoajax.js',function(json){
var str = 'Test with a select populated by json data:<br /><select name="myfield" id="myfield">';
$.each(json,function(i,obj){
str += '<option value="'+ obj.value +'">'+ obj.name +'</option>';
});
str += '</select>';
$.prompt(str,{
callback: function(v,m,f){ //just print out the selection
$.prompt('You chose value '+ f.myfield);
}
});
});
}
$.getJSON('demoajax.js',function(json){
var str = 'Test with a select populated by json data:<br /><select name="myfield" id="myfield">';
$.each(json,function(i,obj){
str += '<option value="'+ obj.value +'">'+ obj.name +'</option>';
});
str += '</select>';
$.prompt(str,{
callback: function(v,m,f){ //just print out the selection
$.prompt('You chose value '+ f.myfield);
}
});
});
}
例子5:Demo States - Loan Calculator
http://trentrichardson.com/Impromptu/demos/loan_calculator.html
Code
最后是一段csss样式,默认前缀jqi,修改时替换前缀进行修改
Code
莫愁前路无知己,天下无人不识君。