php 如何用JS代码加载php文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19994463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load php file with JS code
提问by gree_tejas
Since I am new to JS coding and web designing, I have a following doubt:
由于我是 JS 编码和网页设计的新手,我有以下疑问:
I have one html page which has onclick function for form text area. If text area remains empty and user clicks submit, it should display message to enter entry and if entry is right one the corresponding file should get open. I have if/else condition in JS file, which is giving appropriate message while text area remains empty but not loading file which I am mentioning when entry is not empty.
我有一个 html 页面,它具有表单文本区域的 onclick 功能。如果文本区域保持空白并且用户单击提交,则应显示输入条目的消息,如果条目正确,则应打开相应的文件。我在 JS 文件中有 if/else 条件,它在文本区域保持为空时给出适当的消息,但不加载我在条目不为空时提到的文件。
Below is the html snippet:
下面是 html 片段:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset=utf-8>
</head>
<body>
<form name="drugform" action="#">
<pre> Drug Name<input type="text" name="name1" value="" /></pre>
</p>
<input type="button" value="Enter" onclick='buttoncheck()' />
</form>
<script type="text/javascript" src="if-else-button.js"></script>
</body>
</html>
and JS code is:
和JS代码是:
function buttoncheck() {
if (Boolean(document.drugform.name1.value)) {
load(form1.php);
} else {
alert('Enter Drug name');
}
}
How to load file with js code (mentioned in if condition)?
如何使用js代码加载文件(在if条件中提到)?
回答by Sohil Desai
回答by power_scriptor
try like this,
像这样尝试,
function buttoncheck() {
if (document.drugform.name1.value != "") { // check not empty condition
load("form1.php"); // passing value to function with quotes
} else {
alert('Enter Drug name');
}
}
回答by sushant-hiray
var url = 'form1.php';
window.location.href = url;
Instead of load(form1.php)
, substitute the above code snippet!
而不是load(form1.php)
,替换上面的代码片段!
EDIT:
编辑:
If you want to load the file without actually redirecting to a new page, you can use jquery's .load()
Check more about it here: http://api.jquery.com/load/
如果您想在不实际重定向到新页面的情况下加载文件,您可以使用 jquery 的.load()
在此处查看更多信息:http: //api.jquery.com/load/