2012-06-04 64 views
0

比方說,我有一個非常通用的jQuery的提示,要求我重新命名一個元素,然後告訴我什麼,我進入:提交了jQuery的提示

$(function() { 
$("#rename").click(function() { 
    jPrompt('Please Enter a New Name:', 'Enter new name...', 'Rename Prompt', function (r) { 
     if (r) { 
      alert('You entered ' + r); 
     } 
    }); 
}); 
}); 

但是,我要重命名的元素存儲在服務器上的數據庫,這意味着我需要運行服務器端腳本來重命名此元素。我認爲這意味着我需要使用提交,就像在表單上一樣。我發現了一些.submit()jQuery函數的信息,但我並不完全明白它是如何工作的。如果任何人都可以讓我知道我將如何進行提交,然後運行一個真正讚賞的服務器端腳本。

回答

1
$(function() { 
    $("#rename").click(function (e) { 
     e.preventDefault(); 
     jPrompt('Please Enter a New Name:', 'Enter new name...', 'Rename Prompt', function (r) { 
      if (r) { 
       $('input[name="NewName"]').val(r.Name); 
       $.post('@Url.Action("Edit", "TheController")', $('#hiddenform').serialize(), function(response) { 
        // response = ActionResult 
       }); 
      } 
     }); 
    }); 
}); 
  1. 使用Url.Action得到一個正確的URI
  2. 使用防止默認阻止的默認操作
  3. 返回一些來自MVC3動作種類JSON的指示成功/失敗。
  4. 將隱藏<form>的地方,並與信息填充後
+0

哇,這是快...感謝了一堆,我完全忘了隱藏的形式。 – RedHack

0

你會想使用AJAX從JavaScript發送一個請求到服務器端腳本。查看jQuery API documentation on AJAX瞭解更多信息。

以下jPrompt回調將向服務器端腳本發出HTTP POST請求,並將idnewName參數與請求一起傳遞。在PHP中,您將使用$_POST['id']$_POST['newName']來檢索這些文件。

function (r) { 
    if (r) { 
     $.post("rename.php", { 
      data: { 
       id: somethingToIdentifyTheElement, 
       newName: r 
      }, 
      success: function(response) { 
       // Successfully renamed the element! 
       // If your server-side script returned something interesting, 
       // you can find it in the response argument 
      } 
     }); 
    } 
} 
0

如果有人可以讓我知道我會怎麼做一個提交,然後運行 服務器端腳本將非常感激。

爲此您需要使用$.ajax()函數,以調用serverside函數。

$.ajax({ 
    url: 'YourController\yourAction', 
    type:'post' 
    success: function(data) { 
    $('.result').html(data); 
    alert('Load was performed.'); 
    } 
});