2013-03-30 58 views
0

我在他們的網站上獲得了這個jQuery UI,並且我想重新制作一個簡單的大學計算應用程序。我的計劃是取出電子郵件,用戶名等基本數據,並用3個主下拉菜單替換它。 1看看他們是否是州內或州外的學生,他們都會有不同的價值觀。另一個下拉菜單會詢問他們是否繼續上大學,並根據他們的投入來更新成本,然後最後一個下拉菜單會詢問他們將參加多少年。根據3個輸入,它計算出該大學的總成本。我需要的是發佈下拉菜單的值而不是文本框。謝謝你的幫助!! 所以我現在有這樣的:需要jQuery UI對話框來更新下拉值

風格:

<style> 
body { font-size: 62.5%; } 
label, input { display:block; } 
input.text { margin-bottom:12px; width:95%; padding: .4em; } 
fieldset { padding:0; border:0; margin-top:25px; } 
h1 { font-size: 1.2em; margin: .6em 0; } 
div#users-contain { width: 350px; margin: 20px 0; } 
div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; } 
div#users-contain table td, div#users-contain table th { border: 1px solid #eee; 
padding: .6em 10px; text-align: left; } 
.ui-dialog .ui-state-error { padding: .3em; } 
.validateTips { border: 1px solid transparent; padding: 0.3em; } 
</style> 

jQuery腳本

<script> 
$(function() { 
var username = $("#username"), 
    studenttut = $("#studenttut"), 
    studenttut = $("#campusrb"), 
    studenttut = $("#yearsatten"), 
    allFields = $([]).add(username).add(studenttut).add(campusrb).add(yearsatten), 
    tips = $(".validateTips"); 

function updateTips(t) { 
    tips 
    .text(t) 
    .addClass("ui-state-highlight"); 
    setTimeout(function() { 
    tips.removeClass("ui-state-highlight", 1500); 
    }, 500); 
} 

function checkLength(o, n, min, max) { 
    if (o.val().length > max || o.val().length < min) { 
    o.addClass("ui-state-error"); 
    updateTips("Length of " + n + " must be between " + 
     min + " and " + max + "."); 
    return false; 
    } else { 
    return true; 
    } 
} 

function checkRegexp(o, regexp, n) { 
    if (!(regexp.test(o.val()))) { 
    o.addClass("ui-state-error"); 
    updateTips(n); 
    return false; 
    } else { 
    return true; 
    } 
} 

$("#dialog-form").dialog({ 
    autoOpen: false, 
    height: 300, 
    width: 350, 
    modal: true, 
    buttons: { 
    "Create an account": function() { 
     var bValid = true; 
     allFields.removeClass("ui-state-error"); 

     bValid = bValid && checkLength(username, "username", 3, 16); 
     bValid = bValid && checkLength(studenttut, "studenttut", 1, 2); 
     bValid = bValid && checkLength(campusrb, "campusrb", 1, 2); 
     bValid = bValid && checkLength(yearsatten, "yearsatten", 1, 2);  
     bValid = bValid && checkRegexp(username, /^[a-z]([0-9a-z_])+$/i, "Username may   consist of a-z, 0-9, underscores, begin with a letter."); 
     bValid = bValid && checkRegexp(studenttut, /^[1-2]/, "Please select an option for the type of student you'll be."); 
     bValid = bValid && checkRegexp(campusrb, /^[1-2]/, "Username may consist of a-z, 0-9, underscores, begin with a letter."); 
     bValid = bValid && checkRegexp(yearsatten, /^[1-8]/, "Username may consist of a-z, 0-9, underscores, begin with a letter."); 

     if (bValid) { 
     $("#users tbody").append("<tr>" + 
      "<td>" + username.val() + "</td>" + 
      "<td>" + studenttut.val() + "</td>" + 
      "<td>" + campusrb.val() + "</td>" + 
      "<td>" + yearsatten.val() + "</td>" + 
     "</tr>"); 
     $(this).dialog("close"); 
     } 
    }, 
    Cancel: function() { 
     $(this).dialog("close"); 
    } 
    }, 
    close: function() { 
    allFields.val("").removeClass("ui-state-error"); 
    } 
}); 

$("#create-user") 
    .button() 
    .click(function() { 
    $("#dialog-form").dialog("open"); 
    }); 
    }); 
    </script> 

HTML

<body> 

<div id="dialog-form" title="Create new user"> 
<p class="validateTips">All form fields are required.</p> 

<form> 
<fieldset> 
<label for="username">username</label> 
<input type="text" name="username" id="username" class="text ui-widget-content ui- corner-all" /> 


<label for="studenttut">Are you a In-state/Out-of-state student?</label> 
<select name="studenttut" id="studenttut" class="text ui-widget-content ui-corner-all" /> 
    <option value="1">In-State-student</option> 
    <option value="2">Out-of-State-student</option> 
</select> 

<label for="campusrb">Are you staying on campus?</label> 
    <select name="campusrb" id="campusrb" class="text ui-widget-content ui- corner-all" /> 
     <option value="" disabled="disabled" selected="selected">Please select an option</option> 
     <option value="1">Yes</option> 
     <option value="2">No</option> 
    </select> 

<label for="yearsatten">How many years are you planning to attend??</label> 
    <select name="yearsatten" id="yearsatten" class="text ui-widget-content ui- corner-all" /> 
     <option value="" disabled="disabled" selected="selected">Please select an option</option>  
     <option value="1">1 year</option> 
     <option value="2">2 years</option> 
     <option value="3">3 years</option> 
     <option value="4">4 years</option> 
     <option value="5">5 years</option> 
     <option value="6">6 years</option> 
     <option value="7">7 years</option> 
     <option value="8">8 years</option> 
    </select> 
</fieldset> 
    </form> 
</div> 



<div id="users-contain" class="ui-widget"> 
    <table id="users" class="ui-widget ui-widget-content"> 
    <thead> 
     <tr class="ui-widget-header "> 
     <th>Total Cost</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 

     </tr> 
    </tbody> 
    </table> 
</div> 
<button id="create-user">Create new user</button> 

</body> 
+0

你現在的代碼有什麼問題?你想在其中改變什麼?對不起,我不明白它.. – kero

+0

當我提交時,它不提交單選按鈕數據僅文本數據。我需要腳本來傳遞值來更新我的頁面。 –

+0

使用這個作爲參考,我希望它是這樣的,但不是提交複選框,我希望單選按鈕被提交。 http://jqueryui.com/dialog/#modal-form –

回答

0

你應該能夠做這樣的事情:

$("#users tbody").append("<tr>" + 
    "<td>" + username.val() + "</td>" + 
    "<td>" + $('#studenttut>option:selected').text() + "</td>" + 
    "<td>" + $('#campusrb>option:selected').text() + "</td>" + 
    "<td>" + $('#yearsatten>option:selected').text() + "</td>" + 
    "</tr>"); 
+0

OMG,謝謝sooo多!!! –