对字符串的操作。拆分并替换为字符串

时间:2018-01-24 19:05:14

标签: javascript html string

当我想检查我的文本类型变量并显示它的每个字符时,它返回值的数字。第二个问题,当我给变量替换它然后它是未定义的。为什么这样,你觉得呢?

<script>

    function checkSpace(x) {
      // alert(x.value.toString());
      for (var charS in x.value.toString()) {
          alert(charS);
      }

      return  x.value.replace(" ", "");
    }


    var wykonawca = document.getElementById("informations").artist;
    var tytul = document.getElementById("informations").title;

    var addd = document.getElementById("tabelkaa");

    var minus = document.getElementById("minus");
    var plus = document.getElementById("plus");
    var row = document.getElementById("effect");


    plus.onclick = function (e) {
      var replacedText1 = checkSpace(wykonawca);
      var replacedText2 = checkSpace(tytul);

      addd.innerHTML = "Artist: " + replacedText1 + "Title: " + replacedText2;
    }
</script>

<body>
  <div id="informationss">
    <form id="informations">
      <p>Wykonawca <input type ="text" name="artist"required> </p>
      <p> Tytul <input type ="text" name="title" required> </p>
    </form> <br/>
    <input type="submit" value="-" id="minus">
    <input type="submit" value="+" id="plus"> <br/>
    <div id="effect"> </div>
  </div>
  <div id ="tabelka">
    <table id="tabelkaa" border="5"></table>
  </div>
</body>

2 个答案:

答案 0 :(得分:0)

charS是索引。 x.value[charS]charS位置的字母。

同样replace(/ /g, "")将替换所有空格。

function checkSpace(x) {
        // alert(x.value.toString());
        for (var charS in x.value) {
            alert(x.value[charS]);
        }

        return  x.value.replace(/ /g, "");
    }

    var wykonawca = document.getElementById("informations").artist;
    var tytul = document.getElementById("informations").title;
    var addd = document.getElementById("tabelkaa");
    var minus = document.getElementById("minus");
    var plus = document.getElementById("plus");
    var row = document.getElementById("effect");

    plus.onclick = function (e) {
        var replacedText1 = checkSpace(wykonawca);
        var replacedText2 = checkSpace(tytul);
        addd.innerHTML = "Artist: " + replacedText1 + " Title: " + replacedText2;
    }
<body>
    <div id="informationss">
        <form id="informations">
            <p>Wykonawca <input type ="text" name="artist"required> </p>
            <p> Tytul <input type ="text" name="title" required> </p>
        </form> 
        <br/>
        <input type="submit" value="-" id="minus">
        <input type="submit" value="+" id="plus"> <br/>
        <div id="effect"></div>
    </div>
    <div id ="tabelka">
        <table id="tabelkaa" border="5">
        </table>
    </div>
</body>

答案 1 :(得分:0)

首先,for..in遍历对象键或数组索引。如果要迭代值,请使用for..of

其次,如果要从字符串中删除所有空格,请使用x.value.replace(/ /g, "")代替x.value.replace(" ", ""),而不是仅替换第一个空格。

<body>
    <div id="informationss">
        <form id="informations">
            <p>Wykonawca <input type ="text" name="artist"required> </p>
            <p> Tytul <input type ="text" name="title" required> </p>
        </form> 
        <br/>
        <input type="submit" value="-" id="minus">
        <input type="submit" value="+" id="plus"> <br/>
        <div id="effect"></div>
    </div>
    <div id ="tabelka">
        <table id="tabelkaa" border="5">
        </table>
    </div>
</body>

<script>
    function checkSpace(x) {
        // alert(x.value.toString());
        for (var charS of x.value.toString()) {
            alert(charS);
        }

        return  x.value.replace(/ /g, "");
    }

    var wykonawca = document.getElementById("informations").artist;
    var tytul = document.getElementById("informations").title;
    var addd = document.getElementById("tabelkaa");
    var minus = document.getElementById("minus");
    var plus = document.getElementById("plus");
    var row = document.getElementById("effect");

    plus.onclick = function (e) {
        var replacedText1 = checkSpace(wykonawca);
        var replacedText2 = checkSpace(tytul);
        addd.innerHTML = "Artist: " + replacedText1 + " Title: " + replacedText2;
    }
</script>