使用 charAt() 的文本分类不起作用

我想区分用户输入的值。


用户使用输入表单来添加值。


如果'#'是用户输入的值的前面。删除'#'并重定向到 A


除了重定向到B


我试图通过使用 charAt() 来区分这一点,但不起作用


我也不知道 .replace() 正在工作


无论我输入什么,该代码只会转到当前 URL + /?。


请帮我


<form>

  <input id="pathID" placeholder="Search ..." +

         style="height:40px; line-height:40px; padding:0 15px;"> 

  <input onclick="return findSharp()" type="submit" 

         value="Search" style="background:#222;">

</form>

<script>

  function findSharp(){

    var stringName = document.getElementById("pathID").value;

    if(stringName.charAt(0) == '#'){

      findURLA()

    } else {

      findURLB()

    }

  }

                       

  function findURLA(){

    var hashId = document.getElementById("pathID").value;

    var hashIdReplace = hashId.replace('#','');

    window.location.href = 

      'http://localhost:8181/activity-2/' + '?' + 

      'hashtag/' + hashIdReplace;

      return false;

  }

                       

  function findeURLB(){

    window.location.href = 'http://localhost:8181/';

    document.write("I AM URL B");

    return false;

  }

</script>


鸿蒙传说
浏览 210回答 3
3回答

慕姐4208626

我在几个地方稍微改变了你的代码给event和event.preventDefault()findSharp 函数从 html 中删除返回语句(更多在这里)向 findURLAA 和 findURLB 添加参数(并修复第二个函数名称中的类型错误)以省略重复读取并消除函数与全局/html 变量的耦合临时注释 findURL-A/B 内容 - 可以取消注释function findSharp(event) {&nbsp; var path = pathID.value;&nbsp; if (path[0] == '#') {&nbsp; &nbsp; findURLA(path)&nbsp; } else {&nbsp; &nbsp; findURLB(path)&nbsp; }&nbsp; event.preventDefault(); // not change location by form}function findURLA(path) {&nbsp; var hashIdReplace = path.replace('#', '');&nbsp; console.log('A:', hashIdReplace)&nbsp; // window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + hashIdReplace;}function findURLB(path) {&nbsp; console.log('B:', path)&nbsp; //window.location.href = 'http://localhost:8181/';&nbsp; //document.write("I AM URL B");}<form>&nbsp; <input id="pathID" placeholder="Search ..." style="height:40px; line-height:40px; padding:0 15px;">&nbsp; <input onclick="findSharp(event)" type="submit" value="Search"></form>

慕盖茨4494581

你可以试试这个。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function findSharp(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var stringName =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("pathID").value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(stringName[0] == '#'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findURLA(stringName.slice(1,))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findURLB(stringName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function findURLA(hashId){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + hashId;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function findURLB(hashId){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.href = 'http://localhost:8181/';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.write("I AM URL B");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

慕村9548890

你的代码在我身上运行良好,也经过测试,唯一的问题是它有一个错误:function findeURLB(){它应该是:function findURLB(){更新:你可以试试这个
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript