为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

鸿蒙传说 2021-10-29 16:08:43
我想区分用户输入的值。用户使用输入表单来添加值。如果'#'是用户输入的值的前面。删除'#'并重定向到 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>
查看完整描述

3 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

我在几个地方稍微改变了你的代码


给event和event.preventDefault()findSharp 函数从 html 中删除返回语句(更多在这里)

向 findURLAA 和 findURLB 添加参数(并修复第二个函数名称中的类型错误)以省略重复读取并消除函数与全局/html 变量的耦合

临时注释 findURL-A/B 内容 - 可以取消注释

function findSharp(event) {

  var path = pathID.value;


  if (path[0] == '#') {

    findURLA(path)

  } else {

    findURLB(path)

  }

  event.preventDefault(); // not change location by form

}


function findURLA(path) {

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

  console.log('A:', hashIdReplace)

  // window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + hashIdReplace;

}


function findURLB(path) {

  console.log('B:', path)

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

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


}

<form>

  <input id="pathID" placeholder="Search ..." style="height:40px; line-height:40px; padding:0 15px;">

  <input onclick="findSharp(event)" type="submit" value="Search">

</form>


查看完整回答
反对 回复 2021-10-29
?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

你可以试试这个。


          function findSharp(){

                var stringName = 

                document.getElementById("pathID").value;

                if(stringName[0] == '#'){

                    findURLA(stringName.slice(1,))

                }

                else{

                    findURLB(stringName)

                }

            }


            function findURLA(hashId){

                window.location.href = 'http://localhost:8181/activity-2/' + '?' + 'hashtag/' + hashId;

            }


            function findURLB(hashId){

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

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

            }


查看完整回答
反对 回复 2021-10-29
?
慕村9548890

TA贡献1884条经验 获得超4个赞

你的代码在我身上运行良好,也经过测试,唯一的问题是它有一个错误:

function findeURLB(){

它应该是:

function findURLB(){

更新:

你可以试试这个


查看完整回答
反对 回复 2021-10-29
  • 3 回答
  • 0 关注
  • 209 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信