dongwu5318 2017-01-22 21:58
浏览 57
已采纳

php html输入文本字段

The user sets through the field an inequality, i.e. 50<A, X>30, 50<C<100. This is stored through javascript function on button click.

My issue is, the field doesn't work in a specific case when there's a "less than" symbol followed by a letter. So, it will store fine e.g. X>30, A<50, but NOT 50<A or 50<C<100. In both of the latter cases it just shows the first number, meaning it's having trouble with the pattern "less than followed by letter", as this is simply omitted. How do I make it work?

Here's my code (php):

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

And javascript part (basically appends all equations entered, separated by ":")

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

1条回答 默认 最新

  • dpkt31779 2017-01-22 22:08
    关注

    Replace the < in your inequalities with &lt; and > with &gt;.
    With < and a letter, the browser is trying to render it as html because it thinks it's a tag (like <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>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了