javascript - php html 输入文本字段

标签 javascript php html

用户通过字段设置不等式,即 50<A, X>30, 50<C<100 。这是通过单击按钮时的 JavaScript 函数存储的。

我的问题是,当“小于”符号后跟一个字母时,该字段在特定情况下不起作用。所以,它会存储得很好,例如X>30, A<50, but NOT 50<A or 50<C<100 。在后两种情况下,它只显示第一个数字,这意味着它在“小于后跟字母”模式方面遇到问题,因为它被简单地省略了。我该如何让它发挥作用?

这是我的代码(php):

echo "<input type=\"text\" id=\"equation\">";
echo "<button onclick=\"addEq()\">Add</button>";

和 javascript 部分(基本上附加输入的所有方程,用“:”分隔)

var addEq = function() {
    eq = document.getElementById("equation").value;
    if (document.getElementById("equation").innerHTML == "") {
        document.getElementById("equation").innerHTML = eq;}
    else {
        document.getElementById("equation").innerHTML += ":" + eq;}}

最佳答案

替换<在你与 &lt; 的不平等中和>&gt; .
<和一封信,浏览器尝试将其呈现为 html,因为它认为它是一个标签(如 <a> )。

var addEq = function() {
    eq = document.getElementById("equation").value.replace(/</g, '&lt;');
    if (document.getElementById("result").innerHTML == "") {
        document.getElementById("result").innerHTML = eq;
    } else {
        document.getElementById("result").innerHTML += ":" + eq;
    }
}
function getResult() {
    var res = document.getElementById("result").innerHTML.replace(/&lt;/g, '<');
    console.log(res);
    return res;
}
<input type="text" id="equation" value="50<C<100">
<button onclick="addEq()">Add</button>
<button onclick="getResult()">Result</button>
<span id="result"></span>

关于javascript - php html 输入文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41796607/

相关文章:

javascript - socket.io 如何发送到以某些关键字开头的房间

PHP MySQL 字母顺序链接

javascript - 更改检测后在 Canvas 上绘制 angular2 图像

带引号的 Javascript 导致 Blogger 模板中缺少参数

javascript - 将时间 slider 与传单合并

javascript - 使用 ng-repeat 从 2 个不同的 JSON 文件中提取数据

php - html2pdf - TCPDF : word wrap in table cell using zero width space (replaced by '?' in PDF)

php - 如何按日期分组,但在select语句中使用日期作为条件?

php - 获取上一小时和当前小时内插入 MySQL 表的值

html - 如何对齐容器内的图像和导航栏?