首先,我将显示代码。这是2页,我们打电话的第一页" form.php"我们打电话给第二页" display.php"。
form.php的
class display {
var $host;
var $username;
var $password;
var $table;
public function display_public() {
$q = "SELECT * FROM agend_items ORDER BY created DESC LIMIT 3";
$r = mysql_query($q);
if ( $r !== false && mysql_num_rows($r) > 0 ) {
while ( $a = mysql_fetch_assoc($r) ) {
$date = stripslashes($a['date']);
$bodytext = stripslashes($a['bodytext']);
$time = stripslashes($a['time']);
$entry_display .= <<<ENTRY_DISPLAY
<h1>00:59</h1>
<div class="post" name="post" >
<h2 name="date">
$date
</h2>
<h3 class="time">
$time
</h3>
<p id="agenda">
$bodytext
</p>
</div>
ENTRY_DISPLAY;
}
} else {
$entry_display = <<<ENTRY_DISPLAY
<h2> This Page Is Under Construction </h2>
<p>
No entries have been made on this page.
Please check back soon, or click the
link below to add an entry!
</p>
ENTRY_DISPLAY;
}
$entry_display .= <<<ADMIN_OPTION
<p class="admin_link">
<a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
</p>
ADMIN_OPTION;
return $entry_display;
}
public function display_admin() {
return <<<ADMIN_FORM
<form action="{$_SERVER['PHP_SELF']}" method="post">
<label for="date">Date:</label><br />
<input name="date" id="date" type="date" maxlength="8" />
<div class="clear"></div>
<label for="time">Time:</label><br />
<input name="time" id="time" type="time" maxlength="8" />
<div class="clear"></div>
<label for="bodytext">Body Text:</label><br />
<textarea name="bodytext" id="bodytext"></textarea>
<div class="clear"></div>
<input type="submit" value="Input Agenda Item" />
</form>
<br />
<a href="display.php">Back to Home</a>
ADMIN_FORM;
}
public function write($p) {
if ( $_POST['date'] )
$date = mysql_real_escape_string($_POST['date']);
if ( $_POST['time'] )
$time = mysql_real_escape_string($_POST['time']);
if ( $_POST['bodytext'])
$bodytext = mysql_real_escape_string($_POST['bodytext']);
if ( $date && $bodytext && $time ) {
$created = time();
$sql = "INSERT INTO agend_items VALUES('$date','$bodytext','$created','$time')";
return mysql_query($sql);
} else {
return false;
}
}
public function connect() {
mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
return $this->buildDB();
}
private function buildDB() {
$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS agend_items (
date VARCHAR(12),
bodytext TEXT,
created VARCHAR(100),
time VARCHAR(8)
)
MySQL_QUERY;
return mysql_query($sql);
}
}
?>
Display.php的
$(document).ready(function(){
$('button').mousedown(function(){
if ($('h3').text() == "00:59") {
alert("Yup"); return true;}
else { alert("Nope");return false;};
});
});
</script>
</head>
<body>
<?php
include_once('_class/form.php');
$obj = new form();
$obj->host = 'localhost';
$obj->username = '*****';
$obj->password = '*****';
$obj->table = 'agend_items';
$obj->connect();
if ( $_POST )
$obj->write($_POST);
echo ( $_GET['admin'] == 1 ) ? $obj->display_admin() : $obj->display_public();
?>
<?php
setlocale(LC_ALL,"hu_HU.UTF8");
echo(strftime("%Y. %B %d. %A. %X %Z"));
?>
<button>Check Work</button>
输出是: 00:59(h1) 2013-05-24(h2) 00:59(h3) 这是文字(p)
我遇到的问题是,如果我运行jquery脚本,它将始终在使用$ time,$ date,$ bodytext创建的h2,h3,p上返回false。
如果在h1中我只是告诉php h1是&#34; 00:59&#34;,代码执行完美。
如果在h3中输出的时间是00:59,并且我使用jquery搜索它,则每次都返回false,如果我搜索date或bodytext,则相同。
如果代码搜索由php变量$ time,$ date,$ bodytext之一输出的文本,似乎代码每次都会失败。我无法弄清楚为什么它找不到这个。 DOM元素应该都可以找到..
你可以忽略h1,它只是插入来检查jquery脚本是否以最简单的形式工作。仅在定位使用从数据库中获取的信息创建的元素文本
时答案 0 :(得分:2)
尝试将每个元素放在一行中,因此H2应该像这样写,没有换行符:
<h2 name="date">$date</h2>
这可能有助于.text找到完全没有被额外空格或行尾字符跳过的日期字符串。
我还建议您使用console.log()来查看它实际看到的内容:
console.log( $('h2').text() );