如何将文本框值获取到jquery变量并进行弹出确认?

时间:2015-03-03 13:15:00

标签: jquery html

我试图将文本框值设置为弹出确认...

但它不起作用

请帮帮我......



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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

我建议您删除内联提交处理程序并执行此操作。 请注意,您无法发布到.html

&#13;
&#13;
$(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
  });
});
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

您必须使用此

进行更改
<input type="button" value="submit" onclick="myfunction()">

Plus:如果你使用id作为表单元素,那么它将比name更好。

答案 2 :(得分:-1)

Tou想要这样的东西。

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>