2015-03-03 20 views
0

我試圖讓文本框的值,彈出確認...如何讓文本框的值爲jQuery變量並進行彈出確認?

但它不工作

幫我請...

function myfunction() { 
 
    
 
    var first = $('input[name=firstname]').val(); 
 
    var last = $('input[name=lastname]').val(); 
 
    
 
    alert(first); // Shows alert 
 
    
 
    //But here i need to get all the text box values and display it in the popup box 
 
    
 
    // After confirmation(OK) the form will submmit to second page 
 
    
 
    // Incase of cancel it returns to the form 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
 
<form action="second.html" method="post" onsubmit="myfunction()"> 
 
First Name: <input type="text" name="firstname"> 
 
Last name: <input type="text" name="lastname"> 
 
    <input type="submit" value="submit"> 
 
    </form>

+0

你的意思是結合'first'和'last'? – 2015-03-03 13:21:41

+1

你到底需要什麼幫助? – PeterKA 2015-03-03 13:23:29

+0

@PeterKA閱讀代碼中的註釋 – mplungjan 2015-03-03 13:34:08

回答

-1

你必須改變與此

<input type="button" value="submit" onclick="myfunction()"> 

加號:如果您使用表單元素的id,它會比名稱更好。

+1

有什麼問題'

'''onsubmit'?除了它是過時的內聯JS的事實。 – Regent 2015-03-03 13:21:00

+0

我的代碼不會發布,只能運行javascript。 – 2015-03-03 13:26:03

0

我建議您刪除內聯提交處理程序,並改爲執行此操作。 請注意您不能爲.html

$(function() { // on page load 
 
    $("#formId").on("submit", function(e) { // assign submi handler 
 
    var first = $('input[name=firstname]').val(); 
 
    if (first == "") { // test first name 
 
     alert("Please enter first name"); 
 
     return false; 
 
    } 
 
    var last = $('input[name=lastname]').val(); 
 
    if (last == "") { 
 
     alert("Please enter last name"); 
 
     return false; 
 
    } 
 
    // Confirm (could be a proper dialog) 
 
    if (!confirm("You entered\n"+first+" "+last+".\ncorrect ? ")) { 
 
     return false; // cancel submit if not confirmed 
 
    } 
 
    // here the form is allowed to be submitted to second.html 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<form id="formId" action="second.html" method="post"> 
 
    First Name: 
 
    <input type="text" name="firstname">Last name: 
 
    <input type="text" name="lastname"> 
 
    <input type="submit" value="submit"> 
 
</form>

-1

頭想是這樣的。

function myfunction() { 
 
    
 
    var first = $('input[name=firstname]').val(); 
 
    var last = $('input[name=lastname]').val(); 
 
    
 
    if (confirm(first + " " + last)){ 
 
     alert("submit to second.html"); 
 
    }else { 
 
     alert("cancel submiting"); 
 
     return false; 
 
    } 
 
     
 
    
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
 
<form action="second.html" method="post" onsubmit="myfunction()"> 
 
First Name: <input type="text" name="firstname"> 
 
Last name: <input type="text" name="lastname"> 
 
    <input type="submit" value="submit"> 
 
    </form>

+0

這將提交表格,無論回答確認 – mplungjan 2015-03-03 13:33:40

+0

它只是警惕..但我需要確認 – Kevin 2015-03-03 13:38:29

+0

是的,上的代碼應該適應,你明顯不想在確認框後顯示警報。 – 2015-03-03 13:38:32

相關問題