简体   繁体   English

jQuery单击事件无法按预期工作

[英]jQuery click events not working as expected

I want the user after clicking the input field, have the anchor options. 我希望用户在单击输入字段后具有锚点选项。 Thus, I capture the click of these anchors and add the contents of that anchor into the input which was empty, white. 因此,我捕获了这些锚点的点击,并将该锚点的内容添加到空白的空白输入中。 But, the script does not work. 但是,该脚本不起作用。 So, where is the error? 那么,错误在哪里呢? Is the logic wrong? 逻辑错了吗? At where? 在哪里 I followed the examples from the Jquery library documentation, I do not understand the error. 我遵循了Jquery库文档中的示例,但我不了解该错误。 Can you tell me where the error is? 你能告诉我错误在哪里吗?

Translation 翻译

pt-br 'Colegios' to en-us 'schools' pt-br“ Colegios”转为“学校”

pt-br 'Unidades' to en-us 'locations' pt-br“统一”到“我们的位置”

pt-br 'Inscrição' to en-us 'subscription' pt-br'Inscrição'到我们的'subscription'

Actions of my script 1. If the user clicks the input type="text", anchor will be added. 我的脚本的操作 1.如果用户单击输入type =“ text”,将添加锚。 2. If it clicks on that anchor, add the value of the content inside the input. 2.如果单击该锚点,则在输入中添加内容的值。

HTML 的HTML

<input id="incricao" type="text" class="inscricao"/>
<input id="colegios" type="text" class="colegios"/>
<input id="unidades" type="text" class="unidades" />

Javascript/Jquery Javascript / jQuery




$().ready(function(e) {

    $('#input').click(function(){
        $('.inscricao').append('<a href="#" id="presencial" target="_blank" rel="noopener noreferrer">Presencial</a><a href="#"  target="_blank" id="online" rel="noopener noreferrer">Online</a>');
    })

    $('#colegios').click(function(){
        $('.colegios').prepend('<a href="#" id="cdfmaster" target="_blank" rel="noopener noreferrer">CDF Master</a><a href="#" id="meninojesus" target="_blank" rel="noopener noreferrer">Menino Jesus</a><a href="#" id="ethos" target="_blank" rel="noopener noreferrer">Ethos</a>');
    })


    $('#unidades').click(function(){
        $('.unidades').prepend('<a href="#" id="recife" target="_blank" rel="noopener noreferrer">Recife</a><a href="#" id="jabatao" target="_blank" rel="noopener noreferrer">Jabatão</a>');
    })

   $("#presencial").click(function() {
       var valorDoInput0 = $(".inscricao").text();    
    $("#inscricao").val(valorDoInput0);
   });

   $("#online").click(function() {
     var valorDoInput1 = $(".inscricao").text();    
    $("#inscricao").val(valorDoInput1);
    });

   $("#cdfmaster").click(function() {
      var valorDoInput2 = $(".colegios").text();    
    $("#colegios").val(valorDoInput2);

    });

    $("#meninojesus").click(function() {
     var valorDoInput3 = $(".colegios").text();    
    $("#colegios").val(valorDoInput3);
    });

    $("#ethos").click(function() {
     var valorDoInput4 = $(".colegios").text();    
    $("#colegios").val(valorDoInput4);
    });


    $("#recife").click(function() {
        var valorDoInput5 = $(".unidades").text();    
       $("#unidades").val(valorDoInput5);
    });


    $("#jabatao").click(function() {
     var valorDoInput6 = $(".unidades").text();    
    $("#unidades").val(valorDoInput6);
    });

});

First of all, you can't append anything inside an input tag, try using after instead of append . 首先,您不能在input标签内append任何内容,请尝试使用after而不是append

Also $('#input') is wrong and you don't have any input id! 另外$('#input')是错误的,您没有任何输入ID! try changing it to $('#incricao') . 尝试将其更改为$('#incricao')

And finally, why you use both class and id to select your inputs? 最后,为什么同时使用class和id来选择输入? remove those classes and just use ids and select your inputs like $('#ID') 删除这些类,仅使用id并选择$('#ID')类的输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM