提交后页面刷新

时间:2013-08-23 13:45:33

标签: jquery html submit refresh

function set(){
if ($.mobile.activePage.attr('id')=="setpage"){
    $("a[data-icon='gear']").attr("class","ui-btn ui-btn-inline ui-btn-icon-top ui-btn-up-a");
}
else{       
    $.mobile.changePage("set.html");   
}
}
set.js:
function setuser(){return false;}
set.html:
<!DOCTYPE HTML>
<html>
<head>

<meta name="viewport" charset="UTF-8" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" href="css/jquerymobile.css" />
<script src="js/jquery.js"></script>
<script src="js/jquerymobile.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
<script src="js/main.js"></script> 
<link rel="stylesheet" href="themes/b.min.css"/>
<script type="text/javascript" src="PushNotification.js"></script>
<script type="text/javascript" src="js/set.js"></script>
</head>

<body>

<div id="setpage" data-theme="b" data-role="page">

<div class="header" style="z-index: 100; position: fixed; top: 0; left:0; right:0;" data-role="header" >

    <a data-iconpos="notext" data-icon="arrow-l" onclick="some()"></a>
    <p class="logo" align="center">Studio Dottor Ragionier Rossi</p>
</div>

<div class="some" style="border: none;" data-theme="b" data-role="content"> 
<form onsubmit="setuser()" autocomplete="off" id="userForm">
    <div style="border-style: none;" data-role="fieldcontain" class="ui-hide-label">
        <label for="username1">Compili i campi per cambiare username</label>
        <input autocomplete="off" type="text" name="username1" id="username1" value="" placeholder="Username attuale" />
    </div>
    <div style="border-style: none;" data-role="fieldcontain" class="ui-hide-label">
        <label for="username3"></label>
        <input autocomplete="off" type="text" name="username3" id="username3" value="" placeholder="Username nuovo" />
    </div>
    <input type="submit" value="Invio" id="submituser">
</form>

</div>
<div id="zen" class="modal"><!-- Place at bottom of page --></div>
<div style="background-image:-webkit-gradient(linear,left top,left bottom,from( #ffffff ),to( #c4c4c4 )); z-index: 100; position:fixed; bottom: 0; left:0; right:0;"   data-role="footer" >
      <div data-role="navbar">
      <ul>
          <li><a data-icon="home" onclick="home()">Home</a></li>
      <li><a data-icon="info" onclick="contatti()">Contatti</a></li>
          <li><a data-icon="gear" onclick="set()">Impostazioni</a></li>
          </ul>
  </div><!-- /navbar -->
</div>

</body>
</html>

为什么提交后会刷新?我使用jquery和jqm我无法解决这个问题。在应用程序的第一页中,我使用相同的代码进行登录并正常工作。 我来自链接的这个页面我是一个导航栏

2 个答案:

答案 0 :(得分:0)

您需要更改此行:

<form onsubmit="setuser()" autocomplete="off" id="userForm">

对此:

<form onsubmit="return setuser();" autocomplete="off" id="userForm">

由于function setuser()仅仅返回false您的onsubmit需要知道如何处理该false,否则它只会将其视为值而不是命令。

使用jquery,你可以编写一个这样的事件监听器:

// Target your form and add event listener
$('#userForm').on('submit', function(e){

    // Stop form from submitting
    e.preventDefault();

});

修改

<script>
function setuser(){
    return false;
}
</script>
<form onsubmit="return setuser();" autocomplete="off" id="userForm">

答案 1 :(得分:0)

尝试在onclick中调用该函数

这是代码

     <script>
     function setuser(){ 
         $.ajax({
                 url:"  ", // specify the url here
                 type:"  ", // specify which method "POST" request or "GET"
                 data:$("#userForm").serialize(),
                 success:function(result){
                 alert(result);
                }
});
     }
     </script>
        <form autocomplete="off" id="userForm">
            <div style="border-style: none;" data-role="fieldcontain" class="ui-hide-label">
                <label for="username1">Compili i campi per cambiare username</label>
                <input autocomplete="off" type="text" name="username1" id="username1" value="" placeholder="Username attuale" />
            </div>
            <div style="border-style: none;" data-role="fieldcontain" class="ui-hide-label">
                <label for="username3"></label>
                <input autocomplete="off" type="text" name="username3" id="username3" value="" placeholder="Username nuovo" />
            </div>
            <input type="button" value="Invio" id="submituser" onclick="setuser()"/>
        </form>